尝试将数据从C#发送到SQL时出现NullReferenceException
长话短说,我必须将数据从C#表单发送到SQL,但我在这一行中不断得到空引用:尝试将数据从C#发送到SQL时出现NullReferenceException,c#,sql,visual-studio-2012,C#,Sql,Visual Studio 2012,长话短说,我必须将数据从C#表单发送到SQL,但我在这一行中不断得到空引用: adapter.InsertCommand.Parameters["@genre"].Value = textBox1.Text; 当我单击按钮将数据发送到数据库时,会出现错误。它还说: 对未设置“”的对象的引用 我尝试了一些方法来修复它,但我似乎无法找出空值的位置。以下是该表单的完整代码。请记住,如果与此相关,我在同一项目中有更多表单: public partial class Form4 : Form {
adapter.InsertCommand.Parameters["@genre"].Value = textBox1.Text;
当我单击按钮将数据发送到数据库时,会出现错误。它还说:
对未设置“”的对象的引用
我尝试了一些方法来修复它,但我似乎无法找出空值的位置。以下是该表单的完整代码。请记住,如果与此相关,我在同一项目中有更多表单:
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private SqlConnection connection;
private SqlDataAdapter adapter;
private void Form2_Load(object sender, EventArgs e)
{
connection = new SqlConnection("Data Source=USER;Initial Catalog=administracion;Integrated Security=True");
adapter = new SqlDataAdapter();
SqlCommand save = new SqlCommand("insert into genres (genre, genre_description)"+
"values (@genre, @genre_description)", connection);
adapter.InsertCommand = save;
adapter.InsertCommand.Parameters.Add(new SqlParameter("@genre", SqlDbType.VarChar));
adapter.InsertCommand.Parameters.Add(new SqlParameter("@genre_description", SqlDbType.VarChar));
}
private void textBox_TextChanged(object sender, EventArgs e)
{
textBox1.MaxLength = 50;
textBox2.MaxLength = 200;
}
private void button1_Click(object sender, EventArgs e)
{
adapter.InsertCommand.Parameters["@genre"].Value = textBox1.Text; // on this line I get the null reference exception
adapter.InsertCommand.Parameters["@genre_description"].Value = textBox2.Text;
try
{
connection.Open();
adapter.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Genre added to database", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SqlException exception)
{
MessageBox.Show(exception.ToString());
}
finally
{
connection.Close();
}
}
}
我是这个特殊编程语言的新手,所以如果这是一个非常基本的问题,我想道歉(可能是)查看代码,我怀疑您没有连接加载事件;因为加载事件处理程序正在创建SQL连接、insert命令等,所以它们为null,因为它没有被调用,并且您正在按钮单击事件处理程序中访问它们 为了证明理论改变了这一点:
public Form4()
{
InitializeComponent();
}
为此:
public Form4()
{
InitializeComponent();
this.Load += Form4_Load;
this.Closed += Form4_Closed;
}
添加以下内容:
void Form4_Closed(object sender, EventArgs e)
{
this.Load -= Form4_Load;
this.Closed -= Form4_Closed;
}
改变这个
private void Form2_Load(object sender, EventArgs e)
对此
private void Form4_Load(object sender, EventArgs e)
因为您的表单名为Form4,而不是Form2(我假设您是从项目中的另一个表单中复制的)。如果在行上放置断点,哪个对象为空?可能是重复的,不确定这是否会导致它,但在
“插入到流派(流派,流派描述)”+“值中缺少空格(@genre,@genre\u description)
介于“genre\u description”
和值之间“
2种可能的情况:adapter
未初始化,或者textBox1
未初始化。您能否保证在用户可以启动按钮1\u单击之前调用Form2\u Load
?在引发异常的行上放置一个断点,查看adapter
和textBox1
的值是什么,您处于Form4
中,但有一个方法Form2\u Load
正在进行初始化。有人给它打电话吗?你想对了,为了节省时间,我从同一个项目的另一个类复制了它,但我没有意识到这是个错误。它现在运转良好。非常感谢你的帮助!!如果通过Visual Studio UI执行,事件处理程序添加/删除代码(+=/-=)在InitializeComponent()方法中,因此有时很难找到它。是的,我正在疯狂地试图找出代码到底出了什么问题,主要是因为我对C#没有太多经验(现在你可能已经知道了),我以前很难处理这个问题,所以我想从现在开始我应该更加关注这类事情。无论如何,谢谢你的时间和答案,伙计!这真的帮了大忙!!