2011-11-25 159 views
0

我一直在試圖弄清代碼有什麼問題。ExecuteNonQuery溢出錯誤?

我想要做的是,有兩個單選按鈕的性別,男&女...

我想,單擊時男時單選按鈕,文本男性在社會性別保存到數據庫字段,如果文本類型,而是我得到溢出錯誤...

添加單選按鈕和[性別]字段之前,一切工作正常...

所以它任何幫助?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace OfflineRF 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     string gender; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string ORF1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\\OfflineRF.mdb"; 
      OleDbConnection conn = new OleDbConnection(ORF1); 
      conn.Open(); 
      OleDbCommand cmd = new OleDbCommand(); 
      cmd.Connection = conn; 
      cmd.CommandText = "INSERT INTO OFFRF([Fname], [Lname], [NIC], [Gender], [HomeTel], [Cellphone], [Passengers], [From], [To])VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + textBox7.Text + textBox8.Text +"','"+gender+"','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "')"; 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 
      System.Windows.Forms.MessageBox.Show("Form Saved Successfully !", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 

      textBox1.Text = ""; 
      textBox2.Text = ""; 
      textBox3.Text = ""; 
      textBox4.Text = ""; 
      textBox5.Text = ""; 
      textBox6.Text = ""; 
      textBox7.Text = ""; 
      textBox8.Text = ""; 
      comboBox1.SelectedIndex = -1; 
      comboBox2.SelectedIndex = -1; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     } 

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (comboBox1.Text == "Karachi") 
      { 
       comboBox2.Items.Clear(); 
       comboBox2.Items.Add("Sukkur"); 
       comboBox2.Items.Add("Hyderabad"); 
      } 
      else if (comboBox1.Text == "Sukkur") 
      { 
       comboBox2.Items.Clear(); 
       comboBox2.Items.Add("Karachi"); 
       comboBox2.Items.Add("Hyderabad"); 
      } 
      else 
      { 
       comboBox2.Items.Clear(); 
       comboBox2.Items.Add("Karachi"); 
       comboBox2.Items.Add("Sukkur"); 
      } 
     } 

     private void Male_CheckedChanged(object sender, EventArgs e) 
     { 
      if (Male.Checked) 
       gender = "Male"; 
      else 
       gender = "Female"; 
     } 

    } 
} 
+0

_什麼是錯誤message_? – SLaks

+0

overflowtenonquery(); –

+0

你的意思是** Stack **溢出?請發佈_exact_錯誤消息。 – SLaks

回答

1

你有一個無限循環。當組合框更改selectedindex事件時,則更改組合框,並且事件再次觸發。

編輯...

在您更改組合框指標再次導致事件觸發的無限循環按鈕的事件處理程序的結束,從SQL-的可能性隨之而來的計算器

+0

在添加單選按鈕之前,它工作正常。 –

1

除了通過在串聯字符串中添加值來注入,如果有人要輸入像「O'Conner」這樣的名稱值,那麼名稱中的引號會終止字符串並將其餘部分關閉。

調查OleDbParameter對象並設置它們。沒有確切的語法,你會做類似

string ORF1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\\OfflineRF.mdb"; 
OleDbConnection conn = new OleDbConnection(ORF1); 
conn.Open(); 
OleDbCommand cmd = new OleDbCommand(); 
cmd.Connection = conn; 
cmd.CommandText = "INSERT INTO OFFRF(Fname, Lname, NIC, Gender, HomeTel, " 
      + "Cellphone, Passengers, [From], [To]) VALUES " 
      + "( ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 
// Add parameters in same ordinal position as the "?" place-holders 
// the first parameter is more of generic description of WHAT it is for and 
// does NOT have to exactly match the column name, the second parameter is 
// the actual value that should be put into the database. This same context 
// is used for performing other SQL actions (select, delete, update, etc) 
// to help prevent SQL injection. 
cmd.Parameters.Add("valForFName", textBox1.Text); 
cmd.Parameters.Add("valForLName", textBox2.Text); 
cmd.Parameters.Add("valForNIC", textBox3.Text + textBox7.Text + textBox8.Text); 
// Not sure of syntax here, but get proper text from your radio choice of gender into string 
gender = YourForm.RadioForGender.SelectedItem.Text; 
cmd.Parameters.Add("valForGender", gender); 
cmd.Parameters.Add("valHomePhone", textBox4.Text); 
cmd.Parameters.Add("valCell", textBox5.Text); 
cmd.Parameters.Add("howmany", textBox6.Text); 
cmd.Parameters.Add("forFromValue", comboBox1.Text); 
cmd.Parameters.Add("forToValue", comboBox2.Text); 
cmd.ExecuteNonQuery(); 
conn.Close();