得到这个错误,已经做了一些研究,但我无法修复它,如果有人可以帮助我:
错误:
WindowsFormsApplication2.exe中出现未处理的“System.NullReferenceException”类型异常
附加信息:对象引用未定义为对象的实例。
它发生在con.Open();
我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
SqlConnection con;
SqlDataAdapter adap;
DataSet ds;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
AdicionarFornecedor add = new AdicionarFornecedor();
add.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
SqlConnection con;
SqlDataAdapter adap;
DataSet ds;
con = new SqlConnection();
con.ConnectionString = (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Duarte\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
adap = new SqlDataAdapter("select * from Pagamentos", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "P");
dataGridView1.DataSource = ds.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show("Erro\n" + ex.Message, "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
private void button4_Click(object sender, EventArgs e)
{
AdicionarPagamento add2 = new AdicionarPagamento();
add2.ShowDialog();
}
private void button6_Click(object sender, EventArgs e)
{
VerFornecedores add = new VerFornecedores();
add.ShowDialog();
}
private void button5_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(item.Index);
}
}
private void search()
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Pagamentos where name like ('" + textBox1.Text + "%')";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
private void textBox1_TextChanged (object sender, EventArgs e)
{
search();
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
我想在写一封信后立即进行实时搜索,程序显示以该字母开头的数据。
答案 0 :(得分:0)
您正在为Form1_Load
中的连接对象创建新引用。在Form_Load方法中尝试这个:
try
{
SqlDataAdapter adap;
DataSet ds;
this.con = new SqlConnection();
this.con.ConnectionString = (@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Duarte\Documents\Visual Studio 2013\Projects\WindowsFormsApplication2\WindowsFormsApplication2\PAPPloran.mdf;Integrated Security=True;Connect Timeout=30");
this.con.Open();
adap = new SqlDataAdapter("select * from Pagamentos", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "P");
dataGridView1.DataSource = ds.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show("Erro\n" + ex.Message, "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
或在搜索方法中创建一个新实例。