4

I am working on a database management system. I have a simple task of updating user profile. I created an asp.net page with textboxes and a save button. After adding the text I click on the save button. The code for the button is

protected void Button1_Click(object sender, EventArgs e)
    {
        string firstName = TextBox2.Text;
        string lastName = TextBox1.Text;
        string sCourse = TextBox3.Text;
        string sTelephone = TextBox4.Text;
        string sAddress = TextBox5.Text;
        string sEmail = TextBox6.Text;
        string Gender = TextBox7.Text;
        string user = User.Identity.Name;

        OleDbConnection oleDBConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\ASPNetDB.accdb");
        string sqlQuerry = "UPDATE aspnet_Users SET firstName=@firstName, lastName=@lastName, Gender=@Gender, Address=@Address, Telephone=@Telephone, Course=@Course, Email=@email WHERE UserName=@UserName";

        OleDbCommand cmd = new OleDbCommand(sqlQuerry, oleDBConn);

        cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);
        cmd.Parameters.AddWithValue("@firstName", firstName);
        cmd.Parameters.AddWithValue("@lastName", lastName);
        cmd.Parameters.AddWithValue("@Course", sCourse);
        cmd.Parameters.AddWithValue("@Telephone", sTelephone);
        cmd.Parameters.AddWithValue("@Address", sAddress);
        cmd.Parameters.AddWithValue("@Gender", Gender);
        cmd.Parameters.AddWithValue("@Email", sEmail);

        oleDBConn.Open();
        cmd.ExecuteNonQuery();
    }

But nothing happens. The database is not updated. Is the code correct?

MI.Sarwar
  • 153
  • 1
  • 3
  • 11

2 Answers2

14

Add the parameter values in the same order as the parameter names appear in the UPDATE statement.

cmd.Parameters.AddWithValue("@firstName", firstName);
cmd.Parameters.AddWithValue("@lastName", lastName);
cmd.Parameters.AddWithValue("@Gender", Gender);
cmd.Parameters.AddWithValue("@Address", sAddress);
cmd.Parameters.AddWithValue("@Telephone", sTelephone);
cmd.Parameters.AddWithValue("@Course", sCourse);
cmd.Parameters.AddWithValue("@Email", sEmail);
cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);

OleDb with Access does not pay attention to the parameter names, only their order.

HansUp
  • 95,961
  • 11
  • 77
  • 135
  • Wow.. I didn't know Access with OleDb doesn't care parameter names. Is there any specific reason for not accepting names, only their orders? Is it because of OleDb or Access? _+1 of course_ – Soner Gönül Apr 28 '13 at 14:49
  • @SonerGönül I don't know *why* that is so. But I don't believe it's due to an MS Access design choice ... parameter names do have meaning when used in queries run under DAO. The "meaningless" parameter name issue seems to be specific to ADO/OleDb. – HansUp Apr 28 '13 at 15:01
3

add the parameters according to the order in the query

  string sqlQuerry = "UPDATE aspnet_Users SET firstName=@firstName, lastName=@lastName,  Gender=@Gender, Address=@Address, Telephone=@Telephone, Course=@Course, Email=@email WHERE UserName=@UserName";

    OleDbCommand cmd = new OleDbCommand(sqlQuerry, oleDBConn);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@firstName", firstName);
    cmd.Parameters.AddWithValue("@lastName", lastName);
    cmd.Parameters.AddWithValue("@Gender", Gender);
    cmd.Parameters.AddWithValue("@Address", sAddress);
    cmd.Parameters.AddWithValue("@Telephone", sTelephone);
    cmd.Parameters.AddWithValue("@Course", sCourse);
    cmd.Parameters.AddWithValue("@Email", sEmail);
    cmd.Parameters.AddWithValue("@UserName", User.Identity.Name);
Shafqat Masood
  • 2,532
  • 1
  • 17
  • 23