在将记录插入数据库“对象引用未设置为对象实例"中时,错误仍然存在. [英] error persisting while inserting a record in database"object reference not set to an instance of an object"
本文介绍了在将记录插入数据库“对象引用未设置为对象实例"中时,错误仍然存在.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DialogResult dh = MessageBox.Show("DO YOU WANT TO INSERT ?", "CONFIRM YOUR INSERT", System.Windows.Forms.MessageBoxButtons.YesNo);
if (dh != DialogResult.No)
{
SqlCommand cmd = new SqlCommand("exec insertcatego @description,@quality,@quantity,@priceUnit,@vat,@totalprice", con);
cmd.Parameters.AddWithValue("@description", txtDesc.Text);
cmd.Parameters.AddWithValue("@quality", comboBoxQuality.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@quantity", txtQant.Text);
cmd.Parameters.AddWithValue("@priceUnit", txtpUnt.Text);
cmd.Parameters.AddWithValue("@vat", txtVat.Text);
cmd.Parameters.AddWithValue("@totalprice", txtTotP.Text);
con.Close();
con.Open();
cmd.ExecuteNonQuery();
frmcategory_Load(sender, e);
MessageBox.Show("SUCCESSFULL INSERT");
}
}
catch (Exception a)
{
MessageBox.Show(a.Message.ToString());
}
}
推荐答案
更正了您的代码.您缺少将SqlConnection对象分配给SqlCommand命令对象.
Corrected your code. you were missing to assign the SqlConnection object to SqlCommand command object.
DialogResult dh = MessageBox.Show("DO YOU WANT TO INSERT ?", "CONFIRM YOUR INSERT", System.Windows.Forms.MessageBoxButtons.YesNo);
if (dh != DialogResult.No)
{
SqlCommand cmd = new SqlCommand("exec insertcatego @description,@quality,@quantity,@priceUnit,@vat,@totalprice", con);
cmd.Parameters.AddWithValue("@description", txtDesc.Text);
cmd.Parameters.AddWithValue("@quality", comboBoxQuality.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@quantity", txtQant.Text);
cmd.Parameters.AddWithValue("@priceUnit", txtpUnt.Text);
cmd.Parameters.AddWithValue("@vat", txtVat.Text);
cmd.Parameters.AddWithValue("@totalprice", txtTotP.Text);
//con.Close();
cmd.Connection=new SqlConnection("your connectionString here" );//Added line of code
con.Open();
cmd.ExecuteNonQuery();
frmcategory_Load(sender, e);
MessageBox.Show("SUCCESSFULL INSERT");
}
}
catch (Exception a)
{
MessageBox.Show(a.Message.ToString());
}
}
祝您好运!!!
Good luck !!!
有关异常详细信息,请参见以下链接:
对象引用未设置为对象的实例. [
Refer the following link for the exception details:
Object reference not set to an instance of an object.[^]
DialogResult dh = MessageBox.Show("DO YOU WANT TO INSERT ?", "CONFIRM YOUR INSERT", System.Windows.Forms.MessageBoxButtons.YesNo);
if (dh != DialogResult.No)
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO category(description,quality, priceUnit,vat,totalprice) VALUES( @description, @quality, @priceUnit, @vat, @totalprice)", conn);
cmd.Parameters.AddWithValue("@description", txtDesc.Text);
cmd.Parameters.AddWithValue("@quality", comboBoxQuality.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@quantity", txtQant.Text);
cmd.Parameters.AddWithValue("@priceUnit", txtpUnt.Text);
cmd.Parameters.AddWithValue("@vat", txtVat.Text);
cmd.Parameters.AddWithValue("@totalprice", txtTotP.Text);
cmd.ExecuteNonQuery();
con.Close();
frmcategory_Load(sender, e);
MessageBox.Show("SUCCESSFULL INSERT");
}
}
catch (Exception a)
{
MessageBox.Show(a.Message.ToString());
}
}
这篇关于在将记录插入数据库“对象引用未设置为对象实例"中时,错误仍然存在.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文