Jayesh Babu
Jayesh Babu

Reputation: 1449

how to insert values entered in datagridview into database table?

I want to insert the numbers entered in a datagridview to a database table. Whenever I do this, only 0 is entered in the table no matter whatever number is entered in the datagridview. Here is my code..

decimal num=Convert.ToDecimal(amountdataGridView.Rows[0].Cells["amountColumn"].Value);
cmd = new SqlCeCommand("insert into Deposit values('" + num + "')", con);
cmd.ExecuteNonQuery();

Upvotes: 1

Views: 1104

Answers (3)

Masoumeh Karvar
Masoumeh Karvar

Reputation: 801

try 'Text' instead of 'Value', I tried this and it works: SearchResultGridView.Rows[0].Cells[0].Text

note that Convert return 0 if amountdataGridView.Rows[0].Cells["amountColumn"].Value be null. did you check if your value is correct?

Upvotes: 0

Edper
Edper

Reputation: 9322

Try to remove the single quote and num to String using ToString():

cmd = new SqlCeCommand("insert into Deposit values('" + num + "')", con);

So, that it would become

cmd = new SqlCeCommand("insert into Deposit values(" + num.ToString() + ")", con);

Upvotes: 1

lauCosma
lauCosma

Reputation: 154

Make sure num is not null before inserting into database and try to remove the quotes around the value in the insert statement :

cmd = new SqlCeCommand("insert into Deposit values( " + num + ")", con);

And also try to output your sql statement and manualy insert it yourself to see if the insert is ok like:

MessageBox.Show("INSERT INTO Deoposit VALUES("+num+")");

Hope this helps.

Upvotes: 0

Related Questions