string or binary data would be truncated IN Asp.net webpages

Posted by Mandlaa under ASP.NET on 5/21/2013 | Points: 10 | Views : 1012 | Status : [Member] | Replies : 2
My TABLE
CREATE TABLE [dbo].[Consumer2](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CouponType] [varchar](50) NULL,
[ck] [varchar](50) NULL,
[CouponValueType] [varchar](50) NULL,
[CouponValue] [int] NULL,
[ValidUntil] [date] NULL,
[ImageName] [varchar](50) NULL,
[ImagePath] [nvarchar](max) NULL,
[CouponSentDate] [date] NULL
) ON [PRIMARY]

MyCODE
protected void Button1_Click1(object sender, EventArgs e)
{
string s1 = string.Empty;

foreach (ListItem item in this.CheckBoxList1.Items)
{
if (item.Selected)
{
s1 += item + ",";
}
}

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Save images into Images folder
FileUpload1.SaveAs(Server.MapPath("images/" + filename));
//Getting dbconnection from web.config connectionstring
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
//Open the database connection
//SqlConnection con = new SqlConnection(str);

con.Open();
//Query to insert images path and name into database
SqlCommand cmd = new SqlCommand("Insert into Consumer2(ImageName,ImagePath,CouponType,ck,CouponValueType,CouponValue,ValidUntil,CouponSentDate) values(@ImageName,@ImagePath,@CouponType,'" + s1 + "',@CouponValueType,@CouponValue,@ValidUntil,@CouponSentDate)", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);

SqlParameter rcoupontype = new SqlParameter("@CouponType", SqlDbType.VarChar, 20);
rcoupontype.Value = coupontype.SelectedItem.ToString();
cmd.Parameters.Add(rcoupontype);

SqlParameter valuetype = new SqlParameter("@CouponValueType", SqlDbType.VarChar, 50);
valuetype.Value = DropDownList1.Text.ToString();
cmd.Parameters.Add(valuetype);

SqlParameter couponValue = new SqlParameter("@CouponValue", SqlDbType.Int, 50);
couponValue.Value = TextBox1.Text.ToString();
cmd.Parameters.Add(couponValue);

SqlParameter validUntil = new SqlParameter("@ValidUntil", SqlDbType.Date, 100);
//validUntil.Value = TextBox2.Text.ToString();
validUntil.Value = Convert.ToDateTime(TextBox2.Text).ToString("dd-MM-yyyy");

cmd.Parameters.Add(validUntil);



SqlParameter couponSentDate = new SqlParameter("@CouponSentDate", SqlDbType.Date, 100);
//couponSentDate.Value = TextBox3.Text.ToString();
couponSentDate.Value = Convert.ToDateTime(TextBox3.Text).ToString("dd-MM-yyyy");

cmd.Parameters.Add(couponSentDate);

cmd.ExecuteNonQuery();
//Close dbconnection
con.Close();

ERROR:string or binary data would be truncated

Some times inserted successfully some times getting error please help what wrong my above code




Responses

Posted by: Mandlaa on: 5/22/2013 [Member] Starter | Points: 25

Up
0
Down
please give me reply any one

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Prabu_Spark on: 5/22/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Mandlaa,
First execute the query manually in sql server 2005 browser window. You will get the idea which column
your giving wrong values. Its error is due to invalid datatype passed in the insert query.
Hope it will helps you.

[Note: Instead of giving values directly in asp.net, first try the insert query with values manually
in sql server browser]

With regards,
J.Prabu.
[Email:prbspark@gmail.com]

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response