Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
2.50/5 (3 votes)
See more:
C#
string cb = "insert into Stock(StockID,ConfigID,Quantity,TotalPrice,StockDate)Values(@StockID,@ConfigID,@Quantity,@TotalPrice,@StockDate)";
 
                cmd = new SqlCommand(cb,con);
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@StockID", txtStockID.Text);
                cmd.Parameters.AddWithValue("@ConfigID",txtConfigID.Text);
                    cmd.Parameters.AddWithValue("@Quantity",txtQty.Text);
                        cmd.Parameters.AddWithValue("@TotalPrice",txtTotalPrice.Text);
                            cmd.Parameters.AddWithValue("@StockDate",dtpStockDate.Value.ToShortDateString());
 
                cmd.ExecuteReader();
Posted
Updated 18-Jun-14 21:25pm
v2
Comments
Prasad Avunoori 19-Jun-14 3:24am    
Where did you get the error? What is the error?
Member 10720633 19-Jun-14 3:26am    
Conversion failed when converting the nvarchar value 'S-311678' to data type int.
note :here S-311678 is the Stock Id which is auto generated
Prasad Avunoori 19-Jun-14 3:30am    
What is the datatype of you StockId?
It should be NVarchar.
agent_kruger 20-Jun-14 0:07am    
sir, please update the question with that error.
agent_kruger 20-Jun-14 0:04am    
sir, what should we do with this code please add some description about what you want or where is the error or somewhat like that.

SQL
Conversion failed when converting the nvarchar value 'S-311678' to data type int.
note :here S-311678 is the Stock Id which is auto generated


I think the error is self explaining. In you Database, StockID is an Integer,and you're trying to store a string. Change the datatype in your database or store a number,without the "S-"
 
Share this answer
 
con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
Share this answer
 
v3
Comments
Sanjay K. Gupta 19-Jun-14 3:38am    
My friend, C# is a case sensitive language. So take care.
Awadhendra Tripathi 19-Jun-14 3:40am    
My Friend you can improve the solution instead of notify me,
but now I improved it thanks :)
Sanjay K. Gupta 19-Jun-14 3:42am    
I am expecting you to do not repeat the same mistakes in future. If I will improve then probably you do the same thing again. Just like, you have improved your solution of first line only rest of two lines still wrong. :) Correct it.
Awadhendra Tripathi 19-Jun-14 3:44am    
instead of writing so much lines you can just write 2 Letters and everything will become perfect.
:)
Sanjay K. Gupta 19-Jun-14 3:46am    
OK. three lines code and four times editing. :)
Instead of writing "cmd.ExecuteReader();" write below code to execute the query

C#
con.Open();
using(con)
{
     cmd.ExecuteNonQuery();
}
 
Share this answer
 
Comments
Thanks7872 19-Jun-14 3:27am    
You would have better opened connection inside using block.
SQL
ALTER TABLE Stock
ALTER COLUMN StockId NVARCHAR(100)
 
Share this answer
 
As error stated

SQL
Conversion failed when converting the nvarchar value 'S-311678' to data type int.
note :here S-311678 is the Stock Id which is auto generated



Either pass integer value to stockID column
note : value 'S-311678' is not inetger

OR change your table column type to NVARCHAR to accept value 'S-311678'
ALTER TABLE Stock
ALTER COLUMN StockId NVARCHAR(100)
 
Share this answer
 
HTML
<pre lang="cs">string cb = "insert into Stock(StockID,ConfigID,Quantity,TotalPrice,StockDate)Values(@StockID,@ConfigID,@Quantity,@TotalPrice,@StockDate)";

                cmd = new SqlCommand(cb,con);
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@StockID", txtStockID.Text);
                cmd.Parameters.AddWithValue("@ConfigID",txtConfigID.Text);
                    cmd.Parameters.AddWithValue("@Quantity",txtQty.Text);
                        cmd.Parameters.AddWithValue("@TotalPrice",txtTotalPrice.Text);
                            cmd.Parameters.AddWithValue("@StockDate",dtpStockDate.Value.ToShortDateString());

                cmd.ExecuteReader();
con.Open();
cmd.ExecuteNonQuery();
con.Close();

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900