با سلام.
من یک فایل excel دارم که میخوام اونو به سی شارپ ایمپورت کنم و اطلاعات خانه های آن را در جدول دیتابیس ذخیره کنم.
فایل من مشکلی نداره در حالت عادی و داده هاش به جدول ارسال میشه اما اگر در فایل Excel یک خانه خارج از جدول مثلا یک اسپس خورده باشد ارور میدهد(البته من حدس میزنم ارور به این علت است چون فرمت همه سلول ها با مقادیر جدول یکسان است) اما این ارور را میدهد:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Conversion failed when converting the nvarchar value ' ' to data type int.
کد import به سی شارپ:
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName); string FolderPath = ConfigurationManager.AppSettings["FolderPath"]; string FilePath = Server.MapPath(FolderPath + FileName); FileUpload1.SaveAs(FilePath); Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text); int code = Convert.ToInt32(Session["code"]); foreach (GridViewRow g1 in GridView3.Rows) { int tedad = 0; int subeshterak = 1; SqlConnection con = new SqlConnection(connect); com = new SqlCommand(@"insert into p_Subscribe(Send,SubEshterak,Total,SubPubliccode,SubStatus,OnvanMoshtarak,SubName,SubFam,Semat,CompanySubject,CompanyName,SubNationalCode,SubPostalCode,SandoghPosti,SubPhone,Fax,SubMobile,SubEmail,Country,SubOstan,SubCity,SubAddress,Subnumbercopies,GroupName,Haghighi,CodeCompany,SubTerm,SubFather,SubDateBirth,Mainstreet,Subline,SubShareDate,SubStartSend,FirstNumber,LastNumber,FishNumber,SubBank,FishDate,FishCost,SubText) values (N'" + tedad + "',N'" + subeshterak + "',N'" + g1.Cells[21].Text.Trim() + "',N'" + code + "',N'" + "False" + "',N'" + g1.Cells[0].Text + "',N'" + g1.Cells[1].Text + "',N'" + g1.Cells[2].Text + "',N'" + g1.Cells[3].Text + "',N'" + g1.Cells[4].Text + "',N'" + g1.Cells[5].Text + "',N'" + g1.Cells[6].Text + "',N'" + g1.Cells[7].Text + "',N'" + g1.Cells[8].Text + "',N'" + g1.Cells[9].Text + "',N'" + g1.Cells[10].Text + "',N'" + g1.Cells[11].Text + "',N'" + g1.Cells[12].Text + "',N'" + g1.Cells[13].Text + "',N'" + g1.Cells[14].Text.Trim() + "',N'" + g1.Cells[15].Text.Trim() + "',N'" + g1.Cells[16].Text + "',N'" + g1.Cells[17].Text.Trim() + "',N'" + g1.Cells[18].Text + "',N'" + g1.Cells[19].Text + "',N'" + g1.Cells[20].Text + "',N'" + g1.Cells[21].Text.Trim() + "',N'" + g1.Cells[22].Text + "',N'" + g1.Cells[23].Text + "',N'" + g1.Cells[24].Text + "',N'" + g1.Cells[25].Text.Trim() + "',N'" + g1.Cells[26].Text + "',N'" + g1.Cells[27].Text + "',N'" + g1.Cells[28].Text + "',N'" + g1.Cells[29].Text + "',N'" + g1.Cells[30].Text + "',N'" + g1.Cells[31].Text + "',N'" + g1.Cells[32].Text + "',N'" + g1.Cells[33].Text + "',N'" + g1.Cells[34].Text + "')", con); con.Open(); com.ExecuteNonQuery(); con.Close(); } BindGrid();
قبل از از Insert کردن باید داده ها را اعتبار سنجی کنید
مثلا چک کنید خالی نباشید یا فرمت مورد نظر شما باشد
با تشکر.
چطور میتونم کل سلول های فایل رو بگردم؟چون ممکنه مقدار نامعتبر یا اسپیس در یک سلول که جزو جدول اصلی نباشه وجود داشته باشه. مثلا یه جا در یک سلول خیلی پرت
در کد مشخصه شما داز گرید دارید Impoert میکنید
در همان حلقه قبل از insert با if چک کنید
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)