|
 |
|
รบกวนผู้รู้ค่ะ.. หนูต้องการใส่ข้อมูลลงใน ฐานข้อมูล โดยใช้ checkBox โดยให้ส่งค่าเป็น True และ False ต้องทำไงหรอค่ะ ขอบคุณค่ะ |
|
 |
|
|
 |
 |
|
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
//this.Hide();
//Form2 f = new Form2();
//f.Show();
//string Chbox1;
int age;
bool isInteger = int.TryParse(checkBox1.Text, out age);
if (isInteger)
{
SqlCommand cw = new SqlCommand("INSERT INTO Document (Doc_01, Doc_02, Doc_03, Doc_05, Doc_06, Doc_07, Doc_08, Doc_09, Doc_10, Doc_11, Doc_12, Doc_13, Doc_14) VALUES(@Doc_01, @Doc_02, @Doc_03, @Doc_05, @Doc_06, @Doc_07, @Doc_08, @Doc_09, @Doc_10, @Doc_11, @Doc_12, @Doc_13, @Doc_14)", cn);
cw.Parameters.AddWithValue("@Doc_01", checkBox1.Text);
cw.Parameters.AddWithValue("@Doc_02", checkBox2.Text);
cw.Parameters.AddWithValue("@Doc_03", checkBox3.Text);
cw.Parameters.AddWithValue("@Doc_04", checkBox4.Text);
cw.Parameters.AddWithValue("@Doc_05", checkBox5.Text);
cw.Parameters.AddWithValue("@Doc_06", checkBox6.Text);
cw.Parameters.AddWithValue("@Doc_07", checkBox67.Text);
cw.Parameters.AddWithValue("@Doc_08", checkBox8.Text);
cw.Parameters.AddWithValue("@Doc_09", checkBox9.Text);
cw.Parameters.AddWithValue("@Doc_10", checkBox10.Text);
cw.Parameters.AddWithValue("@Doc_11", checkBox111.Text);
cw.Parameters.AddWithValue("@Doc_12", checkBox112.Text);
cw.Parameters.AddWithValue("@Doc_13", checkBox113.Text);
cw.Parameters.AddWithValue("@Doc_14", checkBox114.Text);
// if (checkBox1.Checked == true)
//{
// Chbox1="True";
//}
//else if(checkBox1.Checked == false)
//{
// Chbox1 = "False";
//}
//if (checkBox2.Checked == true)
//{
// chk2="True";
//}
//else ( chk2="False" );
try
{
int affectedRows = cw.ExecuteNonQuery();
if (affectedRows > 0)
{
MessageBox.Show("Insert Success!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
checkBox1.Text = "";
}
else
{
MessageBox.Show("Insert failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("No Data Inserted! ", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Tag : .NET, C#

|
|
 |
 |
 |
 |
Date :
2012-08-13 23:59:09 |
By :
nameme18 |
View :
1040 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หนูคิดว่าหนู เขียนผิด นะค่ะ..รบกวนด้วยนะค่ะ ..
ขอบคุณจิงๆนะค่ะ (คิดไม่ออกแล้วค่ะ)
|
 |
 |
 |
 |
Date :
2012-08-14 00:00:59 |
By :
nameme18 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ขึ้นแบบนี้หมายความว่าไงค่ะ
|
ประวัติการแก้ไข 2012-08-14 02:56:59
 |
 |
 |
 |
Date :
2012-08-14 02:53:34 |
By :
nameme18 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันเตือนเฉยๆว่า ประกาศตัวแปรแล้วไม่เรียกใช้ คับ
ขอถามหน่อยคับ ตัวแปรChbox1นี้มีไว้เช็คเออเร่อเฉยๆเหรอ
ส่วนตัวแปร@Doc_01 เอาไว้เก็บชื่อtextของcheckBox เหรอคับ
|
 |
 |
 |
 |
Date :
2012-08-14 10:04:52 |
By :
bigmon |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
หนูพิมพ์ผิดค่ะ..ที่จริงแล้ว..Chbox1 มีไว้เก็บค่า ture กับ false ค่ะ..ตอนนี้ยังงงอยู่ว่าทำไมข้อมูลไม่เข้าไปใน ฐานข้อมูลอะค่ะ..พี่..
หนูติดตรงนี้นานแล้วอะค่ะ...ขอบคุณนะค่ะ
|
 |
 |
 |
 |
Date :
2012-08-14 20:15:03 |
By :
nameme18 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
string Chbox1 = "";
string Chbox2 = "";
string Chbox3 = "";
string Chbox4 = "";
string Chbox5 = "";
string Chbox6 = "";
string Chbox7 = "";
string Chbox8 = "";
string Chbox9 = "";
string Chbox10 = "";
string Chbox11 = "";
string Chbox12 = "";
string Chbox13 = "";
string Chbox14 = "";
int age;
bool isInteger = int.TryParse(checkBox1.Text, out age);
if (isInteger)
{
if (checkBox1.Checked == true)
{
Chbox1 = "True";
}
else if (checkBox1.Checked == false)
{
Chbox1 = "false";
}
if (checkBox2.Checked == true)
{
Chbox2 = "True";
}
else if (checkBox2.Checked == false)
{
Chbox2 = "false";
}
if (checkBox3.Checked == true)
{
Chbox3 = "True";
}
else if (checkBox3.Checked == false)
{
Chbox3 = "false";
}
//string sql = "Insert into Document(Doc_01, Doc_02, Doc_03, Doc_05, Doc_06, Doc_07, Doc_08, Doc_09, Doc_10, Doc_11, Doc_12, Doc_13, Doc_14)values('" + @Chbox1 + "','" + @Chbox2 + "','" + @Chbox3 + "','" + @Chbox3 + "','" + @Chbox4 + "','" + @Chbox5 + "','" + @Chbox6 + "','" + @Chbox7 + "','" + @Chbox8 + "','" + @Chbox9 + "','" + @Chbox10 + "','" + @Chbox11 + "','" + @Chbox12 + "','" + @Chbox13 + "','" + @Chbox14 + "')";
//SqlCommand cmd = new SqlCommand(sql, cn);
SqlCommand cmd = new SqlCommand("INSERT INTO Document (Doc_01, Doc_02, Doc_03, Doc_05, Doc_06, Doc_07, Doc_08, Doc_09, Doc_10, Doc_11, Doc_12, Doc_13, Doc_14) VALUES ('" + @Chbox1 + "','" + @Chbox2 + "','" + @Chbox3 + "','" + @Chbox4 + "','" + @Chbox5 + "','" + @Chbox6 + "','" + @Chbox7 + "','" + @Chbox8 + "','" + @Chbox9 + "','" + @Chbox10 + "','" + @Chbox11 + "','" + @Chbox12 + "','" + @Chbox13 + "','" + @Chbox14 + "')", cn);
cmd.Parameters.AddWithValue("@Chbox1", checkBox1.Text);
cmd.Parameters.AddWithValue("@Chbox2", checkBox2.Text);
cmd.Parameters.AddWithValue("@Chbox3", checkBox3.Text);
cmd.Parameters.AddWithValue("@Chbox4", checkBox4.Text);
cmd.Parameters.AddWithValue("@Chbox5", checkBox5.Text);
cmd.Parameters.AddWithValue("@Chbox6", checkBox6.Text);
cmd.Parameters.AddWithValue("@Chbox7", checkBox67.Text);
cmd.Parameters.AddWithValue("@Chbox8", checkBox8.Text);
cmd.Parameters.AddWithValue("@Chbox9", checkBox9.Text);
cmd.Parameters.AddWithValue("@Chbox10", checkBox10.Text);
cmd.Parameters.AddWithValue("@Chbox11", checkBox111.Text);
cmd.Parameters.AddWithValue("@Chbox12", checkBox112.Text);
cmd.Parameters.AddWithValue("@Chbox13", checkBox113.Text);
cmd.Parameters.AddWithValue("@Chbox14", checkBox114.Text);
cmd.ExecuteNonQuery();
// if (checkBox1.Checked == true)
//{
// Chbox1 = "True";
//}
//else if (checkBox1.Checked == false)
//{
// Chbox1 = "False";
//}
try
{
int affectedRows = cmd.ExecuteNonQuery();
if (affectedRows > 0)
{
MessageBox.Show("Insert Success!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Insert failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("No Data Inserted! ", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//MessageBox.Show(Chbox1, "ผลสำรวจของคุณ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
|
 |
 |
 |
 |
Date :
2012-08-14 20:16:39 |
By :
nameme18 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|