Visual Studio C# datagridview to database -
this question has answer here:
- what nullreferenceexception, , how fix it? 33 answers
i'm trying save data datagridview database. keep on getting
nullreferenceexception: object reference not set instance of object
try { foreach (datagridviewrow row in datagridview2.rows) { using ( conn = new sqlconnection(constring)) { using (sqlcommand cmd = new sqlcommand("insert tbl_students_marks values(@student, @t1, @t2, @t3, @t4)", conn)) { cmd.parameters.addwithvalue("@student", (row.cells["student number"].value).tostring()); cmd.parameters.addwithvalue("@t1", row.cells["test 1"].value); cmd.parameters.addwithvalue("@t2", row.cells["test 2"].value); cmd.parameters.addwithvalue("@t3", row.cells["test 3"].value); cmd.parameters.addwithvalue("@t4", row.cells["test 4"].value); conn.open(); cmd.executenonquery(); conn.close();
looks row value empty.
try replace line 9 code:
var studentnumber = row.cells["student number"].value if (studentnumber == null || studentnumber dbnull) continue; cmd.parameters.addwithvalue("@student", studentnumber.tostring());
Comments
Post a Comment