I am using a try
/ catch
block. The query is executed successfully and gives a message, but after that it gives nullvalue massage for Householdid column which is foreign key, although the text box took the value and data is inserted. I want that this error message is not shown.
private void OK_HouseHoldMember_Click_1(object sender, EventArgs e)
{
//Datagrid view
string ConString = @"Data Source=.;Initial Catalog=HouseHoldProfile;Integrated Security=True";
SqlConnection conn = new SqlConnection(ConString);
conn.Open();
textBox13.Text = textBox3.Text;
try
{
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
textBox13.Text = textBox3.Text;
SqlCommand sqlCommand_3 = new SqlCommand();
sqlCommand_3.CommandType = CommandType.StoredProcedure;
sqlCommand_3.Connection = conn;
sqlCommand_3.CommandText = "InsertHouseHoldMembertbl";
sqlCommand_3.Parameters.AddWithValue("@HouseHoldID", dr.Cells["آی دی خانواده"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@Gender", dr.Cells["جنسیت"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@Age", dr.Cells["سن تکمیل شده"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@MaritalStatus", dr.Cells["حالت مدنی"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@HeadRelationship", dr.Cells["قرابت با رییس خانواده"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@Student", dr.Cells["شاگرد"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@EducationLevelMature", dr.Cells["سطح سواد"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@HighestEducationLevel", dr.Cells["بلند ترین درجه تحصیل"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@job", dr.Cells["شغل اصلی"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@TemporaryJob", dr.Cells["شغل فرعی"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@Disability", dr.Cells["معلولیت"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@Addict", dr.Cells["اعتیاد"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@addictType", dr.Cells["نوعیت اعتیاد"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@Pregnant", dr.Cells["حاملگی"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@MilkChildwoman", dr.Cells["طفل سو تغذی"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@UnderNutrationChild", dr.Cells["طفل سو تغذی"].Value ?? DBNull.Value);
sqlCommand_3.Parameters.AddWithValue("@CauseNotSchool", dr.Cells["علت مکتب نرفتن"].Value ?? DBNull.Value);
sqlCommand_3.ExecuteNonQuery();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
It looks as though you are coercing the value of HouseHoldID
, which might be allowing it to get through without throwing an exception.
I suggest trying to remove the:
?? DBNull.Value
From that line, so that it throws an exception if it's null
, as the value shouldn't be null
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.