本文介绍了对象引用未设置为实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么会出现异常对象引用未设置为实例"?
我的CS代码在下面给出
Why am I getting the exception "Object reference not set to an instance" ?
My cs code is given below
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection cnn;
connetionString = "Data Source=JDPANDEY-PC;Initial Catalog=amsco;User ID = sa;Password= 123";
cnn = new SqlConnection(connetionString);
SqlTransaction tr = null;
cnn.Open();
tr = cnn.BeginTransaction();
try
{
// MessageBox.Show("Connection Open ! ");
SqlCommand cmd = new SqlCommand("insert into registration (eid,ename,fname,dob,age,sex,marital,address,contact,qualification,djoining,jsalary,csalary,designation,department) values (@v1,@v2,@v3,@v4,@v5,@v6,@v7,@v8,@v9,@v10,@v11,@v12,@v13,@v14,@v15)");
//,@cont,@rtype,@tclass,@board,@city,@pcode,@state,@payment)");
cmd.Transaction = tr;
cmd.Parameters.AddWithValue("@v1", textBox1.Text);
cmd.Parameters.AddWithValue("@v2", textBox2.Text);
cmd.Parameters.AddWithValue("@v3", textBox3.Text);
cmd.Parameters.AddWithValue("@v4", textBox4.Text);
cmd.Parameters.AddWithValue("@v5", textBox5.Text);
cmd.Parameters.AddWithValue("@v6", comboBox1.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@v7", comboBox2.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@v8", textBox6.Text);
cmd.Parameters.AddWithValue("@v9", textBox7.Text);
cmd.Parameters.AddWithValue("@v10", comboBox3.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@v11", textBox8.Text);
cmd.Parameters.AddWithValue("@v12", textBox9.Text);
cmd.Parameters.AddWithValue("@v13", textBox10.Text);
cmd.Parameters.AddWithValue("@v14", textBox11.Text);
cmd.Parameters.AddWithValue("@v15", textBox12.Text);
cmd.Connection = cnn;
cmd.ExecuteNonQuery();
tr.Commit();
}
catch (Exception ex)
{
//MessageBox.Show("Can not open connection ! ");
MessageBox.Show(ex.Message);
}
finally
{
cnn.Close();
}
}
推荐答案
这篇关于对象引用未设置为实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!