2011-07-16 31 views
3

一個或多個必需參數給定值,沒有任何一個或多個必需的參數給出了mycommand.ExecuteNonQuery()... 值我不知道是什麼問題... 五月任何人的幫助? THX =)沒有與C#

System.Data.OleDb.OleDbConnection cnregister; 
    System.Data.OleDb.OleDbCommand cnUpreg; 
    System.Data.OleDb.OleDbDataReader ReaderReg; 
    private void cmdregister_Click(object sender, EventArgs e) 
    { 
     cnregister = new System.Data.OleDb.OleDbConnection(); 
     string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=|DataDirectory|Register.mdb"; 
     OleDbConnection myConnection = new OleDbConnection(connectionString); 

     string InputId_reg; 
     string InputPass_reg; 
     InputId_reg = txtuserid_reg.Text; 
     InputPass_reg = txtpass_reg.Text; 
     myConnection.Open(); 


     OleDbCommand cnUpreg = new OleDbCommand("SELECT * FROM tblRegister", myConnection); 

     ReaderReg = cnUpreg.ExecuteReader();  //reader open 
     while (ReaderReg.Read()) 
     { 
      if (InputId_reg == (ReaderReg["UserID"].ToString())) 
      { //to check whether the UserID is same with the ID in the database 
       // if yes, a message box will promt     
       MessageBox.Show("The user had register"); 
       txtuserid_reg.Focus(); 
       txtuserid_reg.Clear(); 
       ReaderReg.Close(); 
       myConnection.Close(); 
       break; 

      } 
      else 
      { 

       string query123 = "INSERT INTO [tblRegister] ([UserID], [Password], [UserName], [UserJob]) VALUES(add1, add2, add3, add4)"; 
       OleDbCommand mycommand = new OleDbCommand(query123, myConnection); 

       mycommand.Parameters.AddWithValue("add1", txtuserid_reg.Text); 
       mycommand.Parameters.AddWithValue("add2", txtpass_reg.Text); 

       mycommand.ExecuteNonQuery(); 
       ReaderReg.Close(); 

       myConnection.Close(); 
       MessageBox.Show("Data save successfully!"); 
       break; 


      } 

     } 

     MessageBox.Show("Break succesffully"); 
    } 
} 

回答

7

您所要求的在查詢4個參數,但只有2分配:

string query123 = "INSERT INTO [tblRegister] ([UserID], [Password], [UserName], [UserJob]) VALUES(add1, add2, add3, add4)"; 
OleDbCommand mycommand = new OleDbCommand(query123, myConnection); 

mycommand.Parameters.AddWithValue("add1", txtuserid_reg.Text); 
mycommand.Parameters.AddWithValue("add2", txtpass_reg.Text); 

// Need UserName and UserJob to satisfy your insert query 

mycommand.ExecuteNonQuery(); 
... 

我不知道你的應用程序是如何設置的,但你需要添加像下面這樣:

mycommand.Parameters.AddWithValue("add3", txtusername_reg.Text); 
mycommand.Parameters.AddWithValue("add4", txtuserjob_reg.Text); 

或更改查詢到這一點:

string query123 = "INSERT INTO [tblRegister] ([UserID], [Password]) VALUES(add1, add2)"; 
+0

OPS之前......解決問題..因爲我必須先嚐試2而不是全部4 ...非常感謝你=) – Cwz90

+0

@ Cwz90:沒問題。有時它只需要第二組眼睛。 –

2

我的猜測是該行設置了查詢與4「參數」

string query123 = "INSERT INTO [tblRegister] ([UserID], [Password], [UserName], [UserJob]) VALUES(add1, add2, add3, add4)"; 

...但你只能增加他們兩個人的ExecuteNonQuery

+0

這意味着應該只添加4而不是2? – Cwz90

+0

我不知道我跟着你。 @ Jason的回答也很好地解釋它 –

+0

謝謝... problem solve =) – Cwz90