Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
DateTime date1 = Convert.ToDateTime(dateTimePicker1.Text);
SqlConnection cnn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");

Change done on this line
C#
SqlCommand cmd = new SqlCommand("insert into subproduct(productcategory,productname,productcode,price,munits,date)values('" + cmdproduct.SelectedValue.ToString() + "','" + txtsub.Text + "','" + txtcode.Text + "'," + txtprice.Text + ",'" + cmdmunits.SelectedValue.ToString() + "','" + date1.ToString() + "')", con);

 cnn.Open();


Comment all this code

C#
 //cmd.Parameters.AddWithValue("@productcategory", cmdproduct.SelectedItem.ToString());
 //cmd.Parameters.AddWithValue("@productname", txtsub.Text);
 //cmd.Parameters.AddWithValue("@productcode", txtcode.Text);
 //cmd.Parameters.AddWithValue("@price", txtprice.Text);
 //cmd.Parameters.AddWithValue("@munits", cmdmunits.SelectedItem.ToString());
 //cmd.Parameters.AddWithValue("@date", date1);

// string dt = (string)dateTimePicker1.Text;
 cnn.Close();

 MessageBox.Show("Record Successfully Generated");


This is the code which i have written on submit button and the error i am getting the exception "String or binary "data would be truncated. The statement has been terminated.

My sql query is

create table subproduct
(
productcategory varchar(50),
productname varchar(50),
productcode varchar(20),
price int,
munits varchar(20),
date datetime
)


Yupiiiiiiii Error Solved Party :laugh: :laugh: :-O :-O :rolleyes: :rolleyes:


Thanks in Advance:rose::rose::rose:
Posted
Updated 22-Oct-10 3:30am
v7
Comments
Sunasara Imdadhusen 21-Oct-10 8:25am    
Please provide value that you have been passed to the procedure.
@nuraGGupta@ 22-Oct-10 1:02am    
Aarti, did u got a solution?
aayu 22-Oct-10 2:36am    
nope :(
@nuraGGupta@ 22-Oct-10 3:35am    
my god, just an insert statement creating a problem for more than 24 hours, why dont you try it with stored procedure.......give it a last try.
aayu 22-Oct-10 5:08am    
I even did that at last i deleted that form and even drop the table from SQL created new form and table but then also same problem i am banging my hand on the PC got so frustrated with this error tried everything but no result god.

Try running the sql insert query from your SQL server query analyzer window. See if the values get inserted to the database, then try with the same set of value from your code...........hope this will let you solve the problem. As you are not using any DAL or BLL, the problem lies in your presentation layer only, so it should not be hard to track it out.
 
Share this answer
 
v2
Comments
aayu 21-Oct-10 7:41am    
i can insert the data in sql by insert statement in same format i have written in C# its giving me error
@nuraGGupta@ 21-Oct-10 7:51am    
Then the problem must be like string instead of int or vice versa, passing to sql server from your code, this problem must be related to '' sort of thing. Why dont you write a store procedure and then execute the stored procedure from your code. Dont forget to write: cmd.CommandType=CommandType.StoredProcedure. Infact, this method must be always followed to avoid such type of situation as I can see, the problem lies somewhere in the insert query only....
Venkatesh Mookkan 21-Oct-10 8:08am    
Anurag, I give up man. You will never make her understand.
@nuraGGupta@ 21-Oct-10 8:14am    
Hey venkatesh, dont give up like this yaar, she had already offered 3 red roses whoever helps her. I am trying my level hard to have those roses, anyways (jokes apart), I have a very hard feeling that the problem lies in the insert query somewhere, and if she can give it try with stored procedure, then I will really be glad....Aarti you stop making those crying faces in blue color.....[:D]
change the following line and try it

SqlCommand cmd = new SqlCommand("insert into subproduct(productcategory ,productname ,productcode,price ,munits,date )values(@productcategory,@productname ,@productcode,@price ,@munits,#date )", cnn);


regards
ak.naser
 
Share this answer
 
v2
Comments
aayu 21-Oct-10 6:40am    
nope it is giving me error for #
You code works perfectly for me.
Please check,

- textBox1.Text returns only numerical values
- cmdmunits.SelectedItem.ToString() return only numerical values
- date1 returns only DateTime (from the code I believe it is numerical only)

I suspect textBox1.Text and cmdunits.SelectedItem

Check if the length of the string exceeds.

Please refer this link http://www.dotnetspider.com/resources/30543-SQL-Error-String-or-binary-data-would-be.aspx[^]

The problem should be in this line.

C#
cmd.Parameters.AddWithValue("@price", textBox1.Text);


price is a int field. Parameters you send using Command are defaulted string datatype. You need to specify the data type for the above line.

Option1:

C#
cmd.Parameters.AddWithValue("@price", Convert.ToInt16(textBox1.Text));


Option 2:
C#
cmd.Parameters.Add(new SqlParameter("@price", SqlDbType.Int));
cmd.Parameters["@price"].Value = Convert.ToInt16(textBox1.Text);


This applies to the date field also.
 
Share this answer
 
v5
Comments
aayu 21-Oct-10 6:44am    
nope i have tried both the option same error
Venkatesh Mookkan 21-Oct-10 6:46am    
Can you give the full trace of your exception? And also please tell me at which line of code you are having the problem?
Venkatesh Mookkan 21-Oct-10 6:55am    
Updated the answer. Please take a look.
aayu 21-Oct-10 6:58am    
the line which i am getting error is cmd.ExecuteNonQuery();
aayu 21-Oct-10 7:01am    
no no no no no no cmdmunits.SelectedItem.ToString() will not return only numerical values
The error exactly suggests the problem. Some of your field length is less than the data you are trying to insert. That’s all. Check all the data length and set max length of the text boxes up to the length of the field.
 
Share this answer
 
Comments
aayu 21-Oct-10 7:06am    
i have checked but same error i not getting where this string or binary is getting problem
Goutam Patra 21-Oct-10 7:09am    
Remove one field from your query and try to execute it. If does not run correctly then remove another field and try again. This way you will find the exact field where you are getting the error.
change the first line and try it


DateTime date1 = Convert.ToDateTime(dateTimePicker1.Value);
 
Share this answer
 
Try To Increase Your DB Column Size's First

Later Try This..
cmd.Parameters.AddWithValue("@price", string.IsNullOrEmpty(textBox1.Text)?0:Convert.ToInt32(textBox1.Text));
cmd.Parameters.AddWithValue("@productcategory",
string.IsNullOrEmpty(cmdproduct.SelectedItem.ToString())?"":cmdproduct.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@munits",
string.IsNullOrEmpty(cmdmunits.SelectedItem.ToString())?"":cmdmunits.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@productname", string.IsNullOrEmpty(txtproductname.Text)?"":txtproductname.Text);
cmd.Parameters.AddWithValue("@productcode", string.IsNullOrEmpty(txtpc.Text)?"":txtpc.Text);

cmd.Parameters.AddWithValue("@price", string.IsNullOrEmpty(textBox1.Text)?"":textBox1.Text);
         cmd.Parameters.AddWithValue("@date", string.IsNullOrEmpty(date1)? Convert.ToDateTime(Datetime.Now):date1);
 
Share this answer
 
v3
Comments
aayu 21-Oct-10 7:12am    
nope same error
aayu 21-Oct-10 7:25am    
no yaar same error :(
Pawan Kiran 21-Oct-10 7:35am    
Atleast,u just try to pass Static Data to Each parameter and Run.

Let me know Record is inserted or not.
aayu 21-Oct-10 7:46am    
pawan when i useing insert statement in SQL it is taking the data no problem only by C# it is giving be string or binary problem
Your Price field is int, are you sure with that?
Are you sure value going is int and not a decimal?
 
Share this answer
 
Comments
aayu 21-Oct-10 8:11am    
yes its int i am not putting and decimal value
nagendrathecoder 21-Oct-10 8:45am    
can you give the query which is executing with original values.
cmd.Parameters.AddWithValue("@price", textBox1.Text);

Replace above line with following one

cmd.Parameters.AddWithValue("@price",Convert.ToInt32(textBox1.Text));

Try this
 
Share this answer
 
DateTime date1 = Convert.ToDateTime(dateTimePicker1.Text);
SqlConnection cnn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");
SqlCommand cmd = new SqlCommand("insert into subproduct(productcategory ,productname ,productcode,price ,munits,date )values(@productcategory,@productname ,@productcode,@price ,@munits,@date )", cnn);

cmd.Parameters.AddWithValue("@productcategory", cmdproduct.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@productname", txtproductname.Text);
cmd.Parameters.AddWithValue("@productcode", txtpc.Text);
cmd.Parameters.AddWithValue("@price", textBox1.Text);
cmd.Parameters.AddWithValue("@munits", cmdmunits.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@date", date1);
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();

MessageBox.Show("Record Successfully Generated
");
 
Share this answer
 
Comments
aayu 21-Oct-10 8:49am    
i have written the same code
nagendrathecoder 21-Oct-10 8:51am    
This is not an answer.
Mohd Wasif 21-Oct-10 8:57am    
Aarti i have used this code in my application its running fine no issues are there .

check it i opened the connection before
means
con.open();
cmd.ExecuteNonQuery();
con.close();
and make your that u r entering same data as per table defination from front end.
Mohd Wasif 21-Oct-10 9:01am    
On which event u have called this code on button event or something else.
Mohd Wasif 21-Oct-10 9:03am    
SqlConnection cnn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");

use this like it.
SqlConnection cnn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\\SQLEXPRESS;Database=MIC;Trusted_Connection= True");

in server name there shud be \\

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