دوستان این کد منه و در خط cmd.excutenonquery() ارور میده ممنون میشم راهنماییم کنید
int id=0; string stime = DateTime.Now.ToString("yyyy-mm-dd hhmmttss"); string filename = Path.GetFileNameWithoutExtension(FileUpload1.FileName); string PicUrl = @"~/imageup/" + filename + "-" +stime + ext; FileUpload1.SaveAs(Server.MapPath(PicUrl)); SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("SELECT user_id FROM user_reg WHERE user_name=@username", con); cmd.Parameters.AddWithValue("@username", Context.User.Identity.Name); con.Open(); id = Convert.ToInt32(cmd.ExecuteScalar()); SqlCommand cmd2 = new SqlCommand("INSERT INTO dbo.image(title,PicAddress,user_id) VALUES (@title,@picAddress,@userid)",con); cmd2.Parameters.AddWithValue("@title", txtTitle.Text); cmd2.Parameters.AddWithValue("@picAddress", PicUrl); cmd2.Parameters.AddWithValue("@userid", id); int result = cmd2.ExecuteNonQuery(); con.Close(); lblresult.ForeColor = System.Drawing.Color.Green; lblresult.Text = "آپلود با موفقیت انجام شد."; pic.Visible = true; ImgUpload.ImageUrl = PicUrl; ImgUpload.Width=217; ImgUpload.Height=139;
چه خطایی؟! متن خطا هم بذار تا بهتر بشه راهنمایی کرد.
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: String or binary data would be truncated. Server Error in '/' Application. String or binary data would be truncated. The statement has been terminated. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated. The statement has been terminated. Source Error: Line 57: cmd2.Parameters.AddWithValue("@picAddress", PicUrl); Line 58: cmd2.Parameters.AddWithValue("@userid", id); Line 59: cmd2.ExecuteNonQuery(); Line 60: con.Close(); Line 61: lblresult.ForeColor = System.Drawing.Color.Green;
برای ذخیره آدرس عکس از کد زیر استفاده کن
savepath = String.Format(Request.PhysicalApplicationPath + imageup\\{0}.{1}", filename + "-" +stime + ext, 'پسوند فایل');
داداش گلم اینو زدم بدتر شد ، میشه اول توضیح بدی این خط کد رو؟
من فک نکنم با این خط مشکل داشتا
تو قسمت user_id حس می کنم مشکل داره
یه سری به دیتابیس بزن ، ببین فیلد های جدول مورد نظرت از چه جنسیه... این خطا موقعی میاد که نوع داده دریافتی با جنس فیلد یکسان نباشه ، یا اگه رشته است طول رشته بیشتر از چیزیه که برای فیلد دیتابیست تعریف کردی..
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)