Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UPDATE query on Access Database not working C#.NET

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?

like image 632
MI.Sarwar Avatar asked Dec 03 '22 23:12

MI.Sarwar


2 Answers

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.

like image 101
HansUp Avatar answered Dec 16 '22 02:12

HansUp


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);
like image 34
Shafqat Masood Avatar answered Dec 16 '22 02:12

Shafqat Masood