this is my code:-
public FRM_MOVES_TRANSFAR()
{
InitializeComponent();
FillComboboxCarPlace();
}
void FillComboboxCarPlace()
{
try
{
dSet = new DataSet();
s = "select Place_id , Place_desc from Place";
sCommand = new SqlCommand(s, con);
sdAdapter = new SqlDataAdapter();
sdAdapter.SelectCommand = sCommand;
sdAdapter.Fill(dSet);
DataRow dr = dSet.Tables[0].NewRow();
dr.ItemArray = new object[2] { 0, " ---Select--- " };
dSet.Tables[0].Rows.InsertAt(dr, 0);
foreach (DataGridViewRow row in dataGridView1.Rows)
{
int index = 5;
var cbxMove = row.Cells[index] as DataGridViewComboBoxCell;
cbxMove.ValueMember = "Place_id";
cbxMove.DisplayMember = "Place_desc";
cbxMove.DataSource = dSet.Tables[0];
}
}
catch
{
return;
}
}
private void btn_save_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.Rows)
{
s = "insert into Move_Transfer (Car_id,busy,damage,withoutDriver,Place_id,fromDate, user_name_save, Save_Date) ";
s = s + " values (@Car_id,@busy,@damage,@withoutDriver,@Place_id,@fromDate ,@user_name_save, CURRENT_TIMESTAMP) ";
sCommand = new SqlCommand(s, con);
sCommand.Parameters.AddWithValue("@Car_id", item.Cells[1].Value.ToString());
sCommand.Parameters.AddWithValue("@busy", item.Cells[2].Value == null ? string.Empty : item.Cells[2].Value.ToString());
sCommand.Parameters.AddWithValue("@damage", item.Cells[3].Value == null ? string.Empty : item.Cells[3].Value.ToString());
sCommand.Parameters.AddWithValue("@withoutDriver", item.Cells[4].Value == null ? string.Empty : item.Cells[4].Value.ToString());
sCommand.Parameters.AddWithValue("@Place_id", item.Cells[5].Value == "NULL" ? string.Empty : item.Cells[5].Value);
sCommand.Parameters.AddWithValue("@fromDate", dateTimePicker_form.Text);
sCommand.Parameters.AddWithValue("@user_name_save", userName);
sCommand.ExecuteNonQuery();
}
}
What I have tried:
my problem when i don't select any value in combobox he gave me error he cant save null value :-
sCommand.Parameters.AddWithValue("@Place_id", item.Cells[5].Value == null ? string.Empty : item.Cells[5].Value);