K
Kalayou
Guest
hi!
I am inserting in bulk of datas to my database. It throws this error: Object reference not set to an instance of object. Here's my code i used to insert data:-
private void saveToolStripButton_Click(object sender, EventArgs e)
{
try
{
using (cmd = new SqlCommand("Insert Into PropertyAssign(Desposition, DateIssued, PropertyID, Description, ModelNbr, SerialNbr, TagNbr, IssCondition, EmpID, eName, Hired, empStatus)Values(@desposition, @IsDate, @AssetID, @description, @model, @serial, @tag, @State, @EID, @name, @HDate, @Status)", con))
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
//cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@desposition", System.Data.SqlDbType.NVarChar, 21, "Desposition"));
cmd.Parameters.Add(new SqlParameter("@IsDate", System.Data.SqlDbType.Date,3, "DateIssued"));
cmd.Parameters.Add(new SqlParameter("@AssetID", System.Data.SqlDbType.NVarChar, 16, "PropertyID"));
cmd.Parameters.Add(new SqlParameter("@description", System.Data.SqlDbType.NVarChar, 100, "Description"));
cmd.Parameters.Add(new SqlParameter("@model", System.Data.SqlDbType.NVarChar, 21, "ModelNbr"));
cmd.Parameters.Add(new SqlParameter("@serial", SqlDbType.NVarChar, 21, "SerialNbr"));
cmd.Parameters.Add(new SqlParameter("@tag", SqlDbType.NVarChar, 21, "TagNbr"));
cmd.Parameters.Add(new SqlParameter("@State", System.Data.SqlDbType.NVarChar, 21, "IssCondition"));
cmd.Parameters.Add(new SqlParameter("@EID", System.Data.SqlDbType.NVarChar, 16, "EmpID"));
cmd.Parameters.Add(new SqlParameter("@name", System.Data.SqlDbType.NVarChar, 50, "eName"));
cmd.Parameters.Add(new SqlParameter("@HDate", System.Data.SqlDbType.Date, 3, "Hired"));
cmd.Parameters.Add(new SqlParameter("@Status", System.Data.SqlDbType.NVarChar, 21, "empStatus"));
cmd.Parameters["@desposition"].Value = dataGridView1.Rows.Cells["Column1"].Value.ToString();
cmd.Parameters["@IsDate"].Value = dataGridView1.Rows.Cells["Column2"].Value.ToString();
cmd.Parameters["@AssetID"].Value = dataGridView1.Rows.Cells["Column3"].Value.ToString();
cmd.Parameters["@description"].Value = dataGridView1.Rows.Cells["Column4"].Value.ToString();
cmd.Parameters["@model"].Value = dataGridView1.Rows.Cells["Column5"].Value.ToString();
cmd.Parameters["@serial"].Value = dataGridView1.Rows.Cells["Column6"].Value.ToString();
cmd.Parameters["@tag"].Value = dataGridView1.Rows.Cells["Column7"].Value.ToString();
cmd.Parameters["@State"].Value = dataGridView1.Rows.Cells["Column8"].Value.ToString();
cmd.Parameters["@EID"].Value = maskedEmpIDTextBox.Text;
cmd.Parameters["@name"].Value = EmpNameTextBox.Text;
cmd.Parameters["@HDate"].Value = HiredateTimePicker.Value.ToString("dd/MM/yyyy");
cmd.Parameters["@Status"].Value = cmbStatus.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
please give me your suggestion so as to resolve my problem. Thank you in advance..
Continue reading...
I am inserting in bulk of datas to my database. It throws this error: Object reference not set to an instance of object. Here's my code i used to insert data:-
private void saveToolStripButton_Click(object sender, EventArgs e)
{
try
{
using (cmd = new SqlCommand("Insert Into PropertyAssign(Desposition, DateIssued, PropertyID, Description, ModelNbr, SerialNbr, TagNbr, IssCondition, EmpID, eName, Hired, empStatus)Values(@desposition, @IsDate, @AssetID, @description, @model, @serial, @tag, @State, @EID, @name, @HDate, @Status)", con))
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
//cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@desposition", System.Data.SqlDbType.NVarChar, 21, "Desposition"));
cmd.Parameters.Add(new SqlParameter("@IsDate", System.Data.SqlDbType.Date,3, "DateIssued"));
cmd.Parameters.Add(new SqlParameter("@AssetID", System.Data.SqlDbType.NVarChar, 16, "PropertyID"));
cmd.Parameters.Add(new SqlParameter("@description", System.Data.SqlDbType.NVarChar, 100, "Description"));
cmd.Parameters.Add(new SqlParameter("@model", System.Data.SqlDbType.NVarChar, 21, "ModelNbr"));
cmd.Parameters.Add(new SqlParameter("@serial", SqlDbType.NVarChar, 21, "SerialNbr"));
cmd.Parameters.Add(new SqlParameter("@tag", SqlDbType.NVarChar, 21, "TagNbr"));
cmd.Parameters.Add(new SqlParameter("@State", System.Data.SqlDbType.NVarChar, 21, "IssCondition"));
cmd.Parameters.Add(new SqlParameter("@EID", System.Data.SqlDbType.NVarChar, 16, "EmpID"));
cmd.Parameters.Add(new SqlParameter("@name", System.Data.SqlDbType.NVarChar, 50, "eName"));
cmd.Parameters.Add(new SqlParameter("@HDate", System.Data.SqlDbType.Date, 3, "Hired"));
cmd.Parameters.Add(new SqlParameter("@Status", System.Data.SqlDbType.NVarChar, 21, "empStatus"));
cmd.Parameters["@desposition"].Value = dataGridView1.Rows.Cells["Column1"].Value.ToString();
cmd.Parameters["@IsDate"].Value = dataGridView1.Rows.Cells["Column2"].Value.ToString();
cmd.Parameters["@AssetID"].Value = dataGridView1.Rows.Cells["Column3"].Value.ToString();
cmd.Parameters["@description"].Value = dataGridView1.Rows.Cells["Column4"].Value.ToString();
cmd.Parameters["@model"].Value = dataGridView1.Rows.Cells["Column5"].Value.ToString();
cmd.Parameters["@serial"].Value = dataGridView1.Rows.Cells["Column6"].Value.ToString();
cmd.Parameters["@tag"].Value = dataGridView1.Rows.Cells["Column7"].Value.ToString();
cmd.Parameters["@State"].Value = dataGridView1.Rows.Cells["Column8"].Value.ToString();
cmd.Parameters["@EID"].Value = maskedEmpIDTextBox.Text;
cmd.Parameters["@name"].Value = EmpNameTextBox.Text;
cmd.Parameters["@HDate"].Value = HiredateTimePicker.Value.ToString("dd/MM/yyyy");
cmd.Parameters["@Status"].Value = cmbStatus.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
please give me your suggestion so as to resolve my problem. Thank you in advance..
Continue reading...