Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am populating Database table by data present in DatagridView cells. When Any of the cells are left blank it gives Exception. i have to assign default value to that cells.
here is my code to Save Data.
C#
string strContName = GVcontacts.Rows[i].Cells[0].Value.ToString();
string strContNo1 = GVcontacts.Rows[i].Cells[1].Value.ToString();
string strContNo2 = GVcontacts.Rows[i].Cells[2].Value.ToString();
string strEmail = GVcontacts.Rows[i].Cells[3].Value.ToString();

string strFillSuppContacts = "Insert into supplier_contacts(supplier_code, Contact_Name, Contact_No1, Contact_No2, Email_ID)"
+ "values('"+lblCode.Text+"','" + strContName + "','" + strContNo1 + "','" + strContNo2 + "','" + strEmail + "')";
MySqlCommand cmdFillSuppContacts = new MySqlCommand(strFillSuppContacts, con);

cmdFillSuppContacts.ExecuteNonQuery();
Posted
Updated 1-Apr-13 22:34pm
v2
Comments
Karthik Harve 2-Apr-13 4:34am    
[Edit] pre tags added.
pradiprenushe 2-Apr-13 4:45am    
Why dont you try this rather than default value from ui
string strContName = GVcontacts.Rows[i].Cells[0].Value.ToString();
strContName = (strContName != null)?strContName : "";
ZurdoDev 2-Apr-13 7:19am    
Value is likely returning an object so when it is null .ToString() throws the exception. You need to check Value first.

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