Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > การใส่ค่าว่างของ integer ในฐานข้อมูล <font color=red>ช่วยดูโค้ดให้หน่อยนะค่ะ ต้องการส่งค่าว่างหรือ null ลงในฐานข้อมูล


 

[.NET] การใส่ค่าว่างของ integer ในฐานข้อมูล <font color=red>ช่วยดูโค้ดให้หน่อยนะค่ะ ต้องการส่งค่าว่างหรือ null ลงในฐานข้อมูล

 
Topic : 042483



โพสกระทู้ ( 8 )
บทความ ( 0 )



สถานะออฟไลน์



ช่วยดูโค้ดให้หน่อยนะค่ะ ต้องการส่งค่าว่างหรือ null ลงในฐานข้อมูล จะต้องทำยังไง

01.protected void Button3_Click(object sender, EventArgs e)
02.   {
03.       ListItemCollection listLine = new ListItemCollection();
04.       listLine.Add(new ListItem(DropDownList11.SelectedItem.Text, DropDownList11.SelectedValue));
05.       listLine.Add(new ListItem(DropDownList12.SelectedItem.Text, DropDownList12.SelectedValue));
06.       listLine.Add(new ListItem(DropDownList13.SelectedItem.Text, DropDownList13.SelectedValue));
07.       listLine.Add(new ListItem(DropDownList14.SelectedItem.Text, DropDownList14.SelectedValue));
08.       listLine.Add(new ListItem(DropDownList15.SelectedItem.Text, DropDownList15.SelectedValue));
09.       listLine.Add(new ListItem(DropDownList16.SelectedItem.Text, DropDownList16.SelectedValue));
10.       listLine.Add(new ListItem(DropDownList17.SelectedItem.Text, DropDownList17.SelectedValue));
11.       listLine.Add(new ListItem(DropDownList18.SelectedItem.Text, DropDownList18.SelectedValue));
12.       listLine.Add(new ListItem(DropDownList19.SelectedItem.Text, DropDownList19.SelectedValue));
13.       listLine.Add(new ListItem(DropDownList20.SelectedItem.Text, DropDownList20.SelectedValue));
14.       listLine.Add(new ListItem(DropDownList21.SelectedItem.Text, DropDownList21.SelectedValue));
15.       listLine.Add(new ListItem(DropDownList22.SelectedItem.Text, DropDownList22.SelectedValue));
16.       listLine.Add(new ListItem(DropDownList33.SelectedItem.Text, DropDownList33.SelectedValue));
17. 
18.       DataSet ds = new DataSet();
19.       DataTable dt = new DataTable();
20. 
21. 
22.       foreach (ListItem item in listLine)
23.       {
24.           dt.Columns.Add(item.Text);
25.       }
26. 
27.       ds.Tables.Add(dt);
28.       ds.AcceptChanges();
29. 
30.       GridView3.DataSource = listLine;
31.       GridView3.DataBind();
32. 
33. 
34.       //นำค่าจาก gridview เก็บในฐานข้อมูล
35.       SqlConnection myConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
36. 
37.       if (myConn.State == ConnectionState.Closed)
38.           myConn.Open();
39.       string sql = "";
40.       SqlTransaction tr = myConn.BeginTransaction();
41.       SqlCommand cmd = myConn.CreateCommand();
42. 
43. 
44.       cmd.CommandType = CommandType.Text;
45.       cmd.Transaction = tr;
46. 
47.       int F01;
48.       int F02;
49.       int F03;
50.       int F04;
51.       int F05;
52.       int F06;
53.       int F07;
54.       int F08;
55.       int F09;
56.       int F10;
57.       int F11;
58.       int F12;
59.       int F13;
60.       F01 = int.Parse(GridView3.Rows[0].Cells[3].Text.Trim());
61.       F02 = int.Parse(GridView3.Rows[1].Cells[3].Text.Trim());
62.       F03 = int.Parse(GridView3.Rows[2].Cells[3].Text.Trim());
63.       F04 = int.Parse(GridView3.Rows[3].Cells[3].Text.Trim());
64.       F05 = int.Parse(GridView3.Rows[4].Cells[3].Text.Trim());
65.       F06 = int.Parse(GridView3.Rows[5].Cells[3].Text.Trim());
66.       F07 = int.Parse(GridView3.Rows[6].Cells[3].Text.Trim());
67.       F08 = int.Parse(GridView3.Rows[7].Cells[3].Text.Trim());
68.       F09 = int.Parse(GridView3.Rows[8].Cells[3].Text.Trim());
69.       F10 = int.Parse(GridView3.Rows[9].Cells[3].Text.Trim());
70.       F11 = int.Parse(GridView3.Rows[10].Cells[3].Text.Trim());
71.       F12 = int.Parse(GridView3.Rows[11].Cells[3].Text.Trim());
72.       F13 = int.Parse(GridView3.Rows[12].Cells[3].Text.Trim());
73.       cmd.CommandText = "Insert Into VendorExportTemplate (VendorNo,SourceID,ExportOption,F01,F02,F03,F04,F05,F06,F07,F08,F09,F10,F11,F12,F13) " +
74. 
75.          " values ('" + DVendor.SelectedValue + "','L','UserDefine','" + F01 + "','" + F02 + "','" + F03 + "','" + F04 + "','" + F05 + "','" + F06 + "','" + F07 + "','" + F08 + "','" + F09 + "','" + F10 + "','" + F11 + "','" + F12 + "','" + F13 + "')";
76.       cmd.ExecuteNonQuery();
77. 
78.       tr.Commit();
79.   }




Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-05-06 14:23:48 By : beauty.coe View : 1748 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

เขียน function ไว้ replace ค่า NULL ให้เป็น 0 น่ะครับ
Date : 2010-05-06 16:17:48 By : webmaster
 

 

No. 2



โพสกระทู้ ( 8 )
บทความ ( 0 )



สถานะออฟไลน์


ให้เป็นศูนย์ไม่ได้ค่ะ เพราะมันจะทับซ้อนกับตัวแปรอื่น
Date : 2010-05-07 09:19:55 By : beauty.coe
 

 

No. 3



โพสกระทู้ ( 373 )
บทความ ( 0 )



สถานะออฟไลน์


ตัวอย่าง int F13;

ปกติการ declare variable int จำเป็นต้องใส่ค่าให้มันก่อนนำไปใช้ เช่น int F13 = 0; หรือ int F13 = -1;

หรือถ้าประกาศตามด้านบน ก็ไม่ผิด แต่ก่อนที่จะนำไปใช้ ยังไงๆ ก็ต้องใส่ค่าให้มันก่อนนำไปใช้ได้

int F13;
F13 = -1; <<< แบบนี้เป็นต้น

F13 = int.Parse(GridView3.Rows[12].Cells[3].Text.Trim());

การใส่ค่าแบบนี้ int.Parse(GridView3.Rows[12].Cells[3].Text.Trim()); ยังไงๆ function int.Parse ก็ return ค่าไม่เป็น null เสมอ นอกจากจะ exception ไปเลย อย่างน้อยมันจะ return 0 หรือไม่ก็ -1

ดังนั้น ถามคุณก่อนว่า ถ้า F13 = 0; มันมีค่าจริงๆ แล้ว F13 = null; มันใส่ไม่ได้ครับ

คุณต้องตรวจสอบตั้งแต่ต้นเลยว่า GridView3.Rows[12].Cells[3].Text.Trim() มันเป็น String.Empty หรือเปล่า แล้วจึงใช้ query นี้แทน

cmd.CommandText = "Insert Into VendorExportTemplate (VendorNo,SourceID,ExportOption,F01,F02,F03,F04,F05,F06,F07,F08,F09,F10,F11,F12,F13) " +

" values ('" + DVendor.SelectedValue + "','L','UserDefine',@F01,@F02,@F03,@F04,@F05,@F06,@F07,@F08,@F09,@F10,@F11,@F12,@F13)";
cmd.Parameters.Add("@F013", SqlDbType.Int).Value = null;
cmd.ExecuteNonQuery();

หวังว่าคงเข้าใจ
Date : 2010-05-07 10:30:15 By : numenoy
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : การใส่ค่าว่างของ integer ในฐานข้อมูล <font color=red>ช่วยดูโค้ดให้หน่อยนะค่ะ ต้องการส่งค่าว่างหรือ null ลงในฐานข้อมูล
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่