c# - argumentnullexception was unhandled insert into -


i'm attempting insert data database via datagridview. code far checks see if table exists, if not creates new table, , copies data it. can copy data table , works well. until error argumentnullexception unhandled. data inserted table, , can view table , see data there. description error:

parameterized query 'insert [alalala_thomas_humphries_quote]([item name], [item description], [retail price], [supplier number], [quantity required], [in stock], [cost price], [total cost],[total retail]) values (@item, @desc,@retail,@supplier,@quantity,@stock,@cost,@totalcost, @totalretail)' expects parameter value not supplied. parameter name: @item

and code:

private void insertdata()         {             string tablename = quotenametxt.text + "_" + firsttxt.text + "_" + surenametxt.text + "_quote";              sqlceconnection con = new sqlceconnection(@"data source=|datadirectory|\lwadatabase.sdf");             con.open();             sqlcedataadapter da = new sqlcedataadapter();              (int = 0; < datagridview1.rows.count - 0; i++)             {                 string query = "insert [" + tablename + "]([item name], [item description], [retail price], [supplier number], [quantity required], [in stock], [cost price], [total cost],[total retail]) values (@item, @desc,@retail,@supplier,@quantity,@stock,@cost,@totalcost, @totalretail)";                 sqlcecommand cmd = new sqlcecommand(query, con);                 cmd.parameters.add(new sqlceparameter("@item", datagridview1.rows[i].cells[0].value));                 cmd.parameters.add(new sqlceparameter("@desc", datagridview1.rows[i].cells[1].value));                 cmd.parameters.add(new sqlceparameter("@retail", datagridview1.rows[i].cells[2].value));                 cmd.parameters.add(new sqlceparameter("@supplier", datagridview1.rows[i].cells[3].value));                 cmd.parameters.add(new sqlceparameter("@quantity", datagridview1.rows[i].cells[4].value));                 cmd.parameters.add(new sqlceparameter("@stock", datagridview1.rows[i].cells[5].value));                 cmd.parameters.add(new sqlceparameter("@cost", datagridview1.rows[i].cells[6].value));                 cmd.parameters.add(new sqlceparameter("@totalcost", datagridview1.rows[i].cells[7].value));                cmd.parameters.add(new sqlceparameter("@totalretail", datagridview1.rows[i].cells[8].value));                 da.insertcommand = cmd;                 cmd.executenonquery();                 messagebox.show("quotation :" + tablename + " saved.", "saved");             }           }   

i'm using c# 2010 , sqlce 3.5.

since data inserted table maybe there casting problem object after data passed database. try converting datagridview1 values proper type before pass them parameters this:

cmd.parameters.add(new sqlceparameter("@item", convert.tostring(datagridview1.rows[i].cells[0].value))); 

i hope helps.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -