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,027

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



 

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

 



Topic : 027148

Guest




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



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


Code (C#)
private void check()

{

//int c = ds.Tables["Program"].Rows.Count - 1;

for (int i = 0; i <= da.Tables["Program"].Rows.Count-1 ; i++)

{


if (da.Tables["Program"].Rows[i]["NameProgram"].ToString().Equals(txtNameProgram.Text.Trim()))

{

MessageBox.Show("คุณป้อนข้อมูลซ้ำ !!! กรุณาป้อนข้อมูลใหม่", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

txtID.Text = "";

txtID.Focus();

txtNameProgram.Text = "";

txtNameProcess.Text = "";

}

}

}

private void Add_Click(object sender, EventArgs e)

{

if (txtID.Text.Trim() == "" || txtNameProgram.Text.Trim() == "" || txtNameProcess.Text.Trim() == "")

{

MessageBox.Show("กรุณากรอกข้อมูลให้ครบ !!!!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

txtID.Text = "";

txtID.Focus();

txtNameProgram.Text = "";

txtNameProcess.Text = "";

return;

}


if (MessageBox.Show("คุณต้องการเพิ่มโปรแกรมใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)

{

check();

strConn = "Data Source=SKZ-43D05CDB745;Initial Catalog=server1;Integrated Security=True; User ID=sa;Password=1234";

Conn = new SqlConnection();

if (Conn.State == ConnectionState.Open)

{

Conn.Close();

}

Conn.ConnectionString = strConn;

Conn.Open();


StringBuilder sb = new StringBuilder();

SqlTransaction tr = Conn.BeginTransaction();


try

{


sb.Remove(0, sb.Length);

sb.Append("INSERT INTO Program (IDProgram,NameProgram,NameProcess)");

sb.Append("VALUES(@IDProgram,@NameProgram, @NameProcess)");


string sqlAdd;

sqlAdd = sb.ToString();


 

SqlCommand com = new SqlCommand();

com.CommandText = sqlAdd;

com.CommandType = CommandType.Text;

com.Connection = Conn;

com.Transaction = tr;

com.Parameters.Clear();


com.Parameters.Add("@IDProgram", SqlDbType.Int).Value = txtID.Text.Trim(); ;

com.Parameters.Add("@NameProgram", SqlDbType.NVarChar).Value = txtNameProgram.Text.Trim();

com.Parameters.Add("@NameProcess", SqlDbType.NVarChar).Value = txtNameProcess.Text.Trim();

com.ExecuteNonQuery();

tr.Commit();







MessageBox.Show("เพิ่มข้อมูลโปรแกรมเรียบร้อยแล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information);

txtID.Text = "";

txtID.Focus();

txtNameProgram.Text = "";

txtNameProcess.Text = "";

ShowData(); 

}

catch (Exception ex)

{

MessageBox.Show("ไม่สามารถเพิ่มข้อมูลโปรแกรมได้ เนื่องจาก " + ex.Message, "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

txtID.Text = "";

txtID.Focus();

txtNameProgram.Text = "";

txtNameProcess.Text = "";

}


}

}


private void ShowData() //Refresh Data Gridview and ListDelete Program

{

if (Conn.State == ConnectionState.Open)

{

Conn.Close();

}

Conn.ConnectionString = strConn;

Conn.Open();

string sqldata = " select * from Program ";

SqlDataAdapter da = new SqlDataAdapter(sqldata, Conn);

da.Fill(ds, "Data1");

dgvProgram.ReadOnly = true;

dgvProgram.DataSource = ds.Tables["Data1"];

string sqldata1 = "select NameProgram from Program";

SqlDataAdapter da1 = new SqlDataAdapter(sqldata1, Conn);

da.Fill(ds, "Data2");

checkedListBox.DataSource = ds.Tables["Data2"];

checkedListBox.DisplayMember = "NameProgram";

checkedListBox.ValueMember = "NameProgram";

checkedListBox.SelectedIndex = 0;

}






Tag : - - - -







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

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 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#)
ลองเช็ค การดึงค่า ว่า 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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

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