Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);
Posted
Updated 29-Aug-16 23:53pm

1 solution

Maybe replace
C#
string.Empty

with
C#
DBNull.Value

The corresponding column will have to be nullable, though.
 
Share this answer
 
Comments
Member 12245539 30-Aug-16 5:56am    
SEND YOUR SQL DATATYPE QUERRY...........
phil.o 30-Aug-16 6:05am    
I'm sorry, what?
Member 12245539 30-Aug-16 6:06am    
Send your sql table so that datatype can be known....
phil.o 30-Aug-16 6:07am    
I have the feeling that you are not talking to the right person.
I don't have any "sql table to send"...
Member 12245539 30-Aug-16 6:08am    
In which table will you store the values?????????

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900