winforms - Issues with Inserting uneven data from datagridview into database c# -
hi have codes insert data datagridview database
for (int = 0; < adodb.rows.count - 1; i++) { sqlcommand cmd = new sqlcommand("insert employee values(@team, @sic, @eid,@employeename,@username,@password,@designation,@department,@email,@hiredate,@takenal,@status)", con); cmd.parameters.addwithvalue("@team", adodb.rows[i].cells[0].value); cmd.parameters.addwithvalue("@sic", adodb.rows[i].cells[1].value); cmd.parameters.addwithvalue("@eid", adodb.rows[i].cells[2].value); cmd.parameters.addwithvalue("@employeename", adodb.rows[i].cells[3].value); cmd.parameters.addwithvalue("@username", adodb.rows[i].cells[4].value); cmd.parameters.addwithvalue("@password", adodb.rows[i].cells[5].value); cmd.parameters.addwithvalue("@designation", adodb.rows[i].cells[6].value); cmd.parameters.addwithvalue("@department", adodb.rows[i].cells[7].value); cmd.parameters.addwithvalue("@email", adodb.rows[i].cells[8].value); cmd.parameters.addwithvalue("@hiredate", adodb.rows[i].cells[9].value); cmd.parameters.addwithvalue("@takenal", adodb.rows[i].cells[10].value); cmd.parameters.addwithvalue("@status", adodb.rows[i].cells[11].value); con.open(); cmd.executenonquery(); con.close(); }
i ask if have 12 columns in datagridview database has 13 columns. prompt error due the difference in things added. how solve problem? in advance
use name reference columns, i.e.
cmd.parameters.addwithvalue("@team", adodb.rows[i].cells["team"].value); cmd.parameters.addwithvalue("@sic", adodb.rows[i].cells["sic"].value); cmd.parameters.addwithvalue("@eid", adodb.rows[i].cells["eid"].value);
the columns receive name database column, when load data database. other way set , name columns via gui in vs, or via code, i.e.:
datagridview1.column[2].name = "eid"
if design column scheme of datagridview in gui or code first, make sure use
datagridview1.autogeneratecolumns = false
and use name reference when loading data, using datapropertyname:
datagridview1.column[2].datapropertyname = "eid"
Comments
Post a Comment