|
 |
|
ช่วยด้วยครับมีปัญหาตอน Add ข้อมูลลง Database ข้ามขั้นเฉยเลย T_T |
|
 |
|
|
 |
 |
|
คือตอน Add ข้อมูลลง Database อ้ะครับ
มันข้ามขั้นตอนจาก Try ไป Catch เลย ช่วยหน่อยครับ
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
string sql = "Select idseat from book where idseat = @IS";
cmd = new SqlCommand(sql, conn);
cmd.Parameters.Clear();
cmd.Parameters.Add("@IS", SqlDbType.NChar).Value = textBox4.Text.Trim();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
int p = adapter.Fill(ds, "b");
if (p == 0)
{
open_conn();
tr = conn.BeginTransaction();
try
{
sql = "Insert into book (idticket, dateticket, idbus, idseat, cuscode) values(@idt, @date, @idb, @ids, @code)";
cmd = new SqlCommand(sql, conn);
cmd.Transaction = tr;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("idt", textBox5.Text);
cmd.Parameters.AddWithValue("date", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("idb", textBox2.Text);
cmd.Parameters.AddWithValue("ids", textBox4.Text);
cmd.Parameters.AddWithValue("code", textBox1.Text);
cmd.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("Saved");
loadData();
clearData();
this.Hide();
FormMenuMain f = new FormMenuMain();
f.Show();
}
catch (Exception)
{
MessageBox.Show("Identify yet");
conn.Close();
}
}
else
{
sql = "Select idbus from book where idseat = @IdB";
cmd = new SqlCommand(sql, conn);
cmd.Parameters.Clear();
cmd.Parameters.Add("@IdB", SqlDbType.NChar).Value = textBox2.Text.Trim();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
int pp = adapter.Fill(ds, "b2");
if (pp == 0)
{
open_conn();
tr = conn.BeginTransaction();
try
{
sql = "Insert into book (idticket, dateticket, idbus, idseat, cuscode) values(@idt, @date, @idb, @ids, @code)";
cmd = new SqlCommand(sql, conn);
cmd.Transaction = tr;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("idt", textBox5.Text);
cmd.Parameters.AddWithValue("date", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("idb", textBox2.Text);
cmd.Parameters.AddWithValue("ids", textBox4.Text);
cmd.Parameters.AddWithValue("code", textBox1.Text);
cmd.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("Saved");
loadData();
clearData();
this.Hide();
FormMenuMain f2 = new FormMenuMain();
f2.Show();
}
catch (Exception)
{
MessageBox.Show("Have a book (bus)");
conn.Close();
}
}
else
{
MessageBox.Show("Have a book"); clearData();
}
}
}
Tag : .NET, C#

|
|
 |
 |
 |
 |
Date :
2014-02-25 08:34:33 |
By :
bewblue12 |
View :
853 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
private void button1_Click(object sender, EventArgs e)
{
string sql = "Select idseat from book where idseat = @IS";
cmd = new SqlCommand(sql, conn);
cmd.Parameters.Clear();
cmd.Parameters.Add("@IS", SqlDbType.NChar).Value = textBox4.Text.Trim();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
int p = adapter.Fill(ds, "b");
if (p == 0)
{
open_conn();
tr = conn.BeginTransaction();
try
{
sql = "Insert into book (idticket, dateticket, idbus, idseat, cuscode) values(@idt, @date, @idb, @ids, @code)";
cmd = new SqlCommand(sql, conn);
cmd.Transaction = tr;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("idt", textBox5.Text);
cmd.Parameters.AddWithValue("date", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("idb", textBox2.Text);
cmd.Parameters.AddWithValue("ids", textBox4.Text);
cmd.Parameters.AddWithValue("code", textBox1.Text);
cmd.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("Saved");
loadData();
clearData();
this.Hide();
FormMenuMain f = new FormMenuMain();
f.Show();
}
catch (Exception)
{
MessageBox.Show("Identify yet");
conn.Close();
}
}
else
{
sql = "Select idbus from book where idseat = @IdB";
cmd = new SqlCommand(sql, conn);
cmd.Parameters.Clear();
cmd.Parameters.Add("@IdB", SqlDbType.NChar).Value = textBox2.Text.Trim();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
int pp = adapter.Fill(ds, "b2");
if (pp == 0)
{
open_conn();
tr = conn.BeginTransaction();
try
{
sql = "Insert into book (idticket, dateticket, idbus, idseat, cuscode) values(@idt, @date, @idb, @ids, @code)";
cmd = new SqlCommand(sql, conn);
cmd.Transaction = tr;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("idt", textBox5.Text);
cmd.Parameters.AddWithValue("date", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("idb", textBox2.Text);
cmd.Parameters.AddWithValue("ids", textBox4.Text);
cmd.Parameters.AddWithValue("code", textBox1.Text);
cmd.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("Saved");
loadData();
clearData();
this.Hide();
FormMenuMain f2 = new FormMenuMain();
f2.Show();
}
catch (Exception)
{
MessageBox.Show("Have a book (bus)");
conn.Close();
}
}
else
{
MessageBox.Show("Have a book"); clearData();
}
}
}
|
 |
 |
 |
 |
Date :
2014-02-25 08:47:38 |
By :
tt |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Debug ?
|
 |
 |
 |
 |
Date :
2014-02-25 08:56:02 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ไขได้แล้วนะครับ
ลองใส่ ex.Message ในcatch เพื่อให้มันบอก error แล้วก็หาแก้ error เรียบร้อยแล้วครับ ^w^
|
 |
 |
 |
 |
Date :
2014-02-25 10:16:03 |
By :
bewblue12 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|