C# ListView对象引用未设置为对象的实例
我的用户控制有问题。当我启动程序时,我得到一个“对象引用未设置为对象的实例。”行“listView1.Items.Add(horario);”中出现错误。我真的不知道该怎么办。也许是个愚蠢的问题,但我找不到。你能帮我吗?我已经尝试过动态使用ItemsSource和xaml方法,但没有成功C# ListView对象引用未设置为对象的实例,c#,wpf,listview,c#-4.0,user-controls,C#,Wpf,Listview,C# 4.0,User Controls,我的用户控制有问题。当我启动程序时,我得到一个“对象引用未设置为对象的实例。”行“listView1.Items.Add(horario);”中出现错误。我真的不知道该怎么办。也许是个愚蠢的问题,但我找不到。你能帮我吗?我已经尝试过动态使用ItemsSource和xaml方法,但没有成功 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用MySql.Data.MySqlClient;
使用System.Windows.Controls;
使用System.Collections.ObjectModel;
名称空间剪贴板\u最终版本
{
公共类Horarios
{
公共字符串hora_entrada{get;set;}
公共字符串hora_saida{get;set;}
公共字符串visto{get;set;}
公共字符串描述符{get;set;}
}
公共部分类Horas:UserControl
{
私有int id_用户;
可观测收集=
新的可观察集合();
公共Horas(内部id用户)
{
id\u user=\u id\u user;
MySqlConnection oi=新的MySqlConnection();
连接字符串=
“服务器=本地主机;”
+“数据库=剪贴板;”
+“uid=职员;”
+“password=clipboardstaff4thewin;”;
尝试
{
oi.Open();
MySqlCommand comando=新的MySqlCommand(“选择horario_entrada、horario_saida、visto、来自horarios的descripao,其中ID_estagario=@ID_user”,oi);
compando.Parameters.Add(“@id\u user”,MySqlDbType.Int32).Value=id\u user;
MySqlDataReader leitor=comando.ExecuteReader();
while(leitor.Read())
{
Horarios horario=(新Horarios
{
hora_entrada=leitor.GetString(0),
hora_saida=leitor.GetString(1),
visto=(leitor.GetChar(0)).ToString(),
descripcao=leitor.GetString(2)
});
listView1.Items.Add(horario);
}
leitor.Close();
}
捕获(MySqlException-ex)
{
MessageBox.Show(“Erro de conexão com o banco de dados.Erro”+ex.ToString());
}
初始化组件();
label3.Content=“Hojeédia”+Convert.ToString(DateTime.Now.Day)+“do mês”+Convert.ToString(DateTime.Now.Month)+“de”+Convert.ToString(DateTime.Now.Year);
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
尝试
{
MySqlConnection oi=新的MySqlConnection();
连接字符串=
“服务器=本地主机;”
+“数据库=剪贴板;”
+“uid=职员;”
+“password=clipboardstaff4thewin;”;
尝试
{
oi.Open();
}
捕获(例外情况除外)
{
MessageBox.Show(“Erro de conexão com o banco de dados.Erro”+ex.ToString());
}
如果(textBox5.Text==“”)
{
MessageBox.Show(“Digite a descripão do dia”);
}
其他的
{
MySqlCommand comando=新的MySqlCommand(“从estagario中选择id_estagario,其中id_funcao=@um”,oi);
compando.Parameters.Add(“@um”,MySqlDbType.Int32).Value=1;
MySqlDataReader leitor=comando.ExecuteReader();
int chefe=0;
while(leitor.Read())
{
chefe=leitor.GetInt32(0);
}
leitor.Close();
int horantrada_inteiro=Convert.ToInt32(textBox1.Text);
int minutosentrada_inteiro=Convert.ToInt32(textBox2.Text);
int horasaida_inteiro=Convert.ToInt32(textBox3.Text);
int minutosaida_inteiro=Convert.ToInt32(textBox4.Text);
如果(((horasaida_inteiro-horacentrada_inteiro)=0&&(minutosaida_inteiro-minutosentrada_inteiro)(DateTime.Now.Hour+1))
{
if(id\u user!=chefe)
{
MessageBox.Show(“Ninguém trabalha nestes horários.”,“Erro”);
MySqlCommand comando1=新的MySqlCommand(“从estagario中选择Nome,其中id_estagario=@id_user”,oi);
comando1.Parameters.Add(“@id\u user”,MySqlDbType.Int32)。Value=id\u user;
MySqlDataReader leitor1=comando1.ExecuteReader();
字符串nome=“”;
while(leitor1.Read())
{
nome=leitor1.GetString(0);
}
leitor1.Close();
leitor1.Close();
comando1=新的MySqlCommand(“插入Recado值(0、@warn、0、'Clipboard SecuritySaver'、@chefe)”,oi);
comando1.Parameters.Add(“@warn”,MySqlDbType.VarChar).Value=“O estagiário”+nome+“tentou burlar O sistemas de horários.”;
comando1.Parameters.Add(“@chefe”,MySqlDbType.Int32)。Value=chefe;
comando1.ExecuteOnQuery();
}
其他的
{
MessageBox.Show(“Chefe querido,vocêdigitou os horários incorrectante”,“Erro”);
}
}
其他的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MySql.Data.MySqlClient;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace Clipboard_final
{
public class Horarios
{
public string hora_entrada{ get; set; }
public string hora_saida { get; set; }
public string visto { get; set; }
public string descricao { get; set; }
}
public partial class Horas : UserControl
{
private int id_user;
ObservableCollection<Horarios> _Horarios =
new ObservableCollection<Horarios>();
public Horas(int _id_user)
{
id_user = _id_user;
MySqlConnection oi = new MySqlConnection();
oi.ConnectionString =
"server=localhost;"
+ "database=clipboard;"
+ "uid=staff;"
+ "password=clipboardstaff4thewin;";
try
{
oi.Open();
MySqlCommand comando = new MySqlCommand("SELECT horario_entrada,horario_saida,visto,descricao from horarios where ID_Estagiario = @id_user", oi);
comando.Parameters.Add("@id_user", MySqlDbType.Int32).Value = id_user;
MySqlDataReader leitor = comando.ExecuteReader();
while (leitor.Read())
{
Horarios horario = (new Horarios
{
hora_entrada = leitor.GetString(0),
hora_saida = leitor.GetString(1),
visto = (leitor.GetChar(0)).ToString(),
descricao = leitor.GetString(2)
});
listView1.Items.Add(horario);
}
leitor.Close();
}
catch (MySqlException ex)
{
MessageBox.Show("Erro de conexão com o banco de dados. Erro " + ex.ToString());
}
InitializeComponent();
label3.Content = "Hoje é dia "+Convert.ToString(DateTime.Now.Day)+" do mês "+Convert.ToString(DateTime.Now.Month)+" de "+Convert.ToString(DateTime.Now.Year);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
MySqlConnection oi = new MySqlConnection();
oi.ConnectionString =
"server=localhost;"
+ "database=clipboard;"
+ "uid=staff;"
+ "password=clipboardstaff4thewin;";
try
{
oi.Open();
}
catch (Exception ex)
{
MessageBox.Show("Erro de conexão com o banco de dados. Erro " + ex.ToString());
}
if (textBox5.Text == "")
{
MessageBox.Show("Digite a descrição do dia");
}
else
{
MySqlCommand comando = new MySqlCommand("Select id_estagiario from estagiario where id_funcao = @um", oi);
comando.Parameters.Add("@um", MySqlDbType.Int32).Value = 1;
MySqlDataReader leitor = comando.ExecuteReader();
int chefe = 0;
while (leitor.Read())
{
chefe = leitor.GetInt32(0);
}
leitor.Close();
int horaentrada_inteiro = Convert.ToInt32(textBox1.Text);
int minutosentrada_inteiro = Convert.ToInt32(textBox2.Text);
int horasaida_inteiro = Convert.ToInt32(textBox3.Text);
int minutossaida_inteiro = Convert.ToInt32(textBox4.Text);
if (((horasaida_inteiro - horaentrada_inteiro) == 0 && (minutossaida_inteiro - minutosentrada_inteiro) <= 0) || horasaida_inteiro - horaentrada_inteiro < 0)
{
MessageBox.Show("Você digitou incorretamente os horários");
}
if (horaentrada_inteiro < 13 || horasaida_inteiro > (DateTime.Now.Hour + 1))
{
if (id_user != chefe)
{
MessageBox.Show("Ninguém trabalha nestes horários.", "Erro");
MySqlCommand comando1 = new MySqlCommand("Select Nome from estagiario where id_estagiario = @id_user", oi);
comando1.Parameters.Add("@id_user", MySqlDbType.Int32).Value = id_user;
MySqlDataReader leitor1 = comando1.ExecuteReader();
string nome = "";
while (leitor1.Read())
{
nome = leitor1.GetString(0);
}
leitor1.Close();
leitor1.Close();
comando1 = new MySqlCommand("Insert Into Recado values(0,@warn,0,'Clipboard SecuritySaver',@chefe)", oi);
comando1.Parameters.Add("@warn", MySqlDbType.VarChar).Value = "O estagiário " + nome + " tentou burlar o sistemas de horários.";
comando1.Parameters.Add("@chefe", MySqlDbType.Int32).Value = chefe;
comando1.ExecuteNonQuery();
}
else
{
MessageBox.Show("Chefe querido, você digitou os horários incorretamente", "Erro");
}
}
else
{
string data = Convert.ToString(DateTime.Now.Day) + '/' + Convert.ToString(DateTime.Now.Month) + '/' + Convert.ToString(DateTime.Now.Year);
string hora_entrada = Convert.ToString(horaentrada_inteiro) + ':' + minutosentrada_inteiro.ToString();
string hora_saida = horasaida_inteiro.ToString() + ':' + minutossaida_inteiro.ToString();
comando = new MySqlCommand("INSERT INTO HORARIOS VALUES (0,@data,@id_user,@hora_entrada,@hora_saida,@descricao,0)", oi);
comando.Parameters.Add("@data", MySqlDbType.VarChar).Value = data;
comando.Parameters.Add("@id_user", MySqlDbType.Int32).Value = id_user;
comando.Parameters.Add("@hora_entrada", MySqlDbType.VarChar).Value = hora_entrada;
comando.Parameters.Add("@hora_saida", MySqlDbType.VarChar).Value = hora_saida;
comando.Parameters.Add("@descricao", MySqlDbType.VarChar).Value = textBox5.Text;
comando.ExecuteNonQuery();
}
}
}
catch (FormatException ex)
{
MessageBox.Show("Escreva somente números");
}
}
private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
}
}