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 > error ช่วยหน่อยคะ...ต้องการตรวจสอบข้อมูลที่รับมาจาก textbox เช็คกับฐานข้อมูลว่าซ้ำหรือไม่


 

[.NET] error ช่วยหน่อยคะ...ต้องการตรวจสอบข้อมูลที่รับมาจาก textbox เช็คกับฐานข้อมูลว่าซ้ำหรือไม่

 
Topic : 027148

Guest



ต้องการตรวจสอบข้อมูลที่รับมาจาก textbox เช็คกับฐานข้อมูลว่าซ้ำหรือไม่
เนื่องจากโค้ดฟ้อง ว่า Object reference not set to an instance of an object. ตรงที่ da.Tables["Program"].Rows.Count-1



ขอความช่วยเหลือชท่านผู้รู้ช่วยแก้ให้หน่อยคะ ขอบคุณล่วงหน้าคะ


Code (C#)
001.private void check()
002. 
003.{
004. 
005.//int c = ds.Tables["Program"].Rows.Count - 1;
006. 
007.for (int i = 0; i <= da.Tables["Program"].Rows.Count-1 ; i++)
008. 
009.{
010. 
011. 
012.if (da.Tables["Program"].Rows[i]["NameProgram"].ToString().Equals(txtNameProgram.Text.Trim()))
013. 
014.{
015. 
016.MessageBox.Show("คุณป้อนข้อมูลซ้ำ !!! กรุณาป้อนข้อมูลใหม่", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
017. 
018.txtID.Text = "";
019. 
020.txtID.Focus();
021. 
022.txtNameProgram.Text = "";
023. 
024.txtNameProcess.Text = "";
025. 
026.}
027. 
028.}
029. 
030.}
031. 
032.private void Add_Click(object sender, EventArgs e)
033. 
034.{
035. 
036.if (txtID.Text.Trim() == "" || txtNameProgram.Text.Trim() == "" || txtNameProcess.Text.Trim() == "")
037. 
038.{
039. 
040.MessageBox.Show("กรุณากรอกข้อมูลให้ครบ !!!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
041. 
042.txtID.Text = "";
043. 
044.txtID.Focus();
045. 
046.txtNameProgram.Text = "";
047. 
048.txtNameProcess.Text = "";
049. 
050.return;
051. 
052.}
053. 
054. 
055.if (MessageBox.Show("คุณต้องการเพิ่มโปรแกรมใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
056. 
057.{
058. 
059.check();
060. 
061.strConn = "Data Source=SKZ-43D05CDB745;Initial Catalog=server1;Integrated Security=True; User ID=sa;Password=1234";
062. 
063.Conn = new SqlConnection();
064. 
065.if (Conn.State == ConnectionState.Open)
066. 
067.{
068. 
069.Conn.Close();
070. 
071.}
072. 
073.Conn.ConnectionString = strConn;
074. 
075.Conn.Open();
076. 
077. 
078.StringBuilder sb = new StringBuilder();
079. 
080.SqlTransaction tr = Conn.BeginTransaction();
081. 
082. 
083.try
084. 
085.{
086. 
087. 
088.sb.Remove(0, sb.Length);
089. 
090.sb.Append("INSERT INTO Program (IDProgram,NameProgram,NameProcess)");
091. 
092.sb.Append("VALUES(@IDProgram,@NameProgram, @NameProcess)");
093. 
094. 
095.string sqlAdd;
096. 
097.sqlAdd = sb.ToString();
098. 
099. 
100.  
101. 
102.SqlCommand com = new SqlCommand();
103. 
104.com.CommandText = sqlAdd;
105. 
106.com.CommandType = CommandType.Text;
107. 
108.com.Connection = Conn;
109. 
110.com.Transaction = tr;
111. 
112.com.Parameters.Clear();
113. 
114. 
115.com.Parameters.Add("@IDProgram", SqlDbType.Int).Value = txtID.Text.Trim(); ;
116. 
117.com.Parameters.Add("@NameProgram", SqlDbType.NVarChar).Value = txtNameProgram.Text.Trim();
118. 
119.com.Parameters.Add("@NameProcess", SqlDbType.NVarChar).Value = txtNameProcess.Text.Trim();
120. 
121.com.ExecuteNonQuery();
122. 
123.tr.Commit();
124. 
125. 
126. 
127. 
128. 
129. 
130. 
131.MessageBox.Show("เพิ่มข้อมูลโปรแกรมเรียบร้อยแล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);
132. 
133.txtID.Text = "";
134. 
135.txtID.Focus();
136. 
137.txtNameProgram.Text = "";
138. 
139.txtNameProcess.Text = "";
140. 
141.ShowData();
142. 
143.}
144. 
145.catch (Exception ex)
146. 
147.{
148. 
149.MessageBox.Show("ไม่สามารถเพิ่มข้อมูลโปรแกรมได้ เนื่องจาก " + ex.Message, "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
150. 
151.txtID.Text = "";
152. 
153.txtID.Focus();
154. 
155.txtNameProgram.Text = "";
156. 
157.txtNameProcess.Text = "";
158. 
159.}
160. 
161. 
162.}
163. 
164.}
165. 
166. 
167.private void ShowData() //Refresh Data Gridview and ListDelete Program
168. 
169.{
170. 
171.if (Conn.State == ConnectionState.Open)
172. 
173.{
174. 
175.Conn.Close();
176. 
177.}
178. 
179.Conn.ConnectionString = strConn;
180. 
181.Conn.Open();
182. 
183.string sqldata = " select * from Program ";
184. 
185.SqlDataAdapter da = new SqlDataAdapter(sqldata, Conn);
186. 
187.da.Fill(ds, "Data1");
188. 
189.dgvProgram.ReadOnly = true;
190. 
191.dgvProgram.DataSource = ds.Tables["Data1"];
192. 
193.string sqldata1 = "select NameProgram from Program";
194. 
195.SqlDataAdapter da1 = new SqlDataAdapter(sqldata1, Conn);
196. 
197.da.Fill(ds, "Data2");
198. 
199.checkedListBox.DataSource = ds.Tables["Data2"];
200. 
201.checkedListBox.DisplayMember = "NameProgram";
202. 
203.checkedListBox.ValueMember = "NameProgram";
204. 
205.checkedListBox.SelectedIndex = 0;
206. 
207.}




Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-05-08 17:30:08 By : error View : 1772 Reply : 5
 

 

No. 1



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

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

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

ตัวแปร da น่าจะไม่ใช่ DataSet น่ะครับ ลองดูดี ๆ ครับ
Date : 2009-05-08 18:38:47 By : webmaster
 

 

No. 2

Guest


ลองเปลี่ยนเป็น ds แล้วคะแต่มันก้อยัง error อยู่ดี
Date : 2009-05-09 11:52:09 By : error
 

 

No. 3

Guest


Code (C#)
1.ลองเช็ค การดึงค่า ว่า ds เป็น null รึเปล่าครับ

Date : 2009-05-11 11:26:58 By : test
 

 

No. 4



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



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


da.Tables["Program"].Rows.Count-1

เช็ค da ว่าค่า null หรือไม่ ถ้าไม่ใช่ null ให้ตรวจสอบตารางฐานข้อมูลที่ใช้ว่ามี field ชื่อ Program อยู่หรือไม่
Date : 2009-05-13 10:19:05 By : theafco
 

 

No. 5



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



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


แก้ไข rep ข้างบนหน่อยครับ
มันต้องเป็น ds ครับ ไม่ใช่ da แล้วเช็คว่ามีโค๊ด da.Fill(ds, "Program"); อยู่ใน scope หรือป่าว
Date : 2009-05-13 10:32:27 By : theafco
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : error ช่วยหน่อยคะ...ต้องการตรวจสอบข้อมูลที่รับมาจาก textbox เช็คกับฐานข้อมูลว่าซ้ำหรือไม่
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 อัตราราคา คลิกที่นี่