Reputation: 31
there was no No value given for one or more required parameters for the mycommand.ExecuteNonQuery()... I wonder what is the problem... May anyone help? thx =)
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");
}
}
Upvotes: 3
Views: 4744
Reputation: 22191
You are asking for 4 parameters in your query, yet only assigning 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();
...
I'm not sure how your application is set up, but you'd need to add something like the following:
mycommand.Parameters.AddWithValue("add3", txtusername_reg.Text);
mycommand.Parameters.AddWithValue("add4", txtuserjob_reg.Text);
Or change your query to this:
string query123 = "INSERT INTO [tblRegister] ([UserID], [Password]) VALUES(add1, add2)";
Upvotes: 7
Reputation: 12051
My guess is this line sets up the query with 4 "parameters"
string query123 = "INSERT INTO [tblRegister] ([UserID], [Password], [UserName], [UserJob]) VALUES(add1, add2, add3, add4)";
...but then you only add two of them before the ExecuteNonQuery
Upvotes: 2