开发者

No value given for one or more required parameters with C#

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");
    }
}


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)";


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜