1

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();
4
  • Do you want to store 1 value or all values in the datagridview? Commented Nov 5, 2013 at 6:28
  • Does your table Deposite only have one column? Commented Nov 5, 2013 at 6:32
  • only 1 value from a particular row.. table consists of only 1 column Commented Nov 5, 2013 at 6:34
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Nov 5, 2013 at 6:37

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

4 Comments

its still not working.. when i enter the value of num as 100, the value entered in the table is 0.
Your table has any other columns, do you have some validation on the database side? What database are you using ? And what type is the Deposit column?
im using ms sql server compact edition 3.5 . column is a float datatype.. there is only 1 column in the table..
try to change from float to decimal datatype because decimal requires more storage space.
0

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?

3 Comments

i checked the value of num. its always null. is there any problem in convertion..?
did you try replacing "amountColumn" with an index (like 0)? replace "amountColumn" with your cell index and try again.
i hav only one cell.. then cell index will be 0. isn't it..?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.