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