Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating information in database

i want to update the user information on database im using c#. here is my code, it doesnt work and it doesn't give me any errors.

protected void Page_Load(object sender, EventArgs e)
{
    lbldisplay.Text = "<b><font color=BLUE>" + "WELLCOME:: " + "</font>" + "<b><font color=white>" + Session["Name"] + "</font>";
    SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=webservice_BuyBid;Integrated Security=True");
}
protected void btnLogOut_Click(object sender, EventArgs e)
{
    //this will redirect the page to the home.aspx page.
    Response.Redirect("Home.aspx");
}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    //creating a new connection to the database
    SqlConnection con = new SqlConnection("Data Source=STUDENT-PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous Processing=True;");
    con.Open();
    //creates a new sqlcommand to update the buyer's information to the Database.
    SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname = @Surname,Email =@Email,CellNumber =@CellNumber", con);
    con.Open();

    cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text);
    cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text);
    cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text);
    cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text);

   int rows = cmd.ExecuteNonQuery();
    con.Close();
like image 245
Sizwe Pellicant Avatar asked Nov 01 '22 06:11

Sizwe Pellicant


1 Answers

Your issue is that you are opening a connection (which is correct) but then you are opening it again.

That is why the update is not occurring. Also you need not close your connection in a finally statement. For the purpose of this, it is not required.

Also you are executing an Update Statement, so please ensure that you give it a condition to update the specific record.

I have edited your code appropriately, this should solve your problem: (Tried and Tested)


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            //creating a new connection to the database
            SqlConnection con = new SqlConnection("Data Source=STUDENT-  PC;Initial Catalog=BuyBid;Integrated Security=True;Asynchronous  Processing=True;");
            con.Open();
            //creates a new sqlcommand to update the buyers information to the   Database.
            SqlCommand cmd = new SqlCommand("UPDATE BUYER SET Name = @Name,Surname  = @Surname,Email =@Email,CellNumber =@CellNumber WHERE Email =@Email", con);
        //con.Open();

            cmd.Parameters.AddWithValue("@Name", txtNameUpdate.Text);
            cmd.Parameters.AddWithValue("@Surname", txtSurnameUpdate.Text);
            cmd.Parameters.AddWithValue("@Email", txtemailUpdate.Text);
            cmd.Parameters.AddWithValue("@CellNumber", txtCellUpdate.Text);
            cmd.BeginExecuteNonQuery();
        }

    catch (Exception e)
    {
        Console.WriteLine("Exception occured: " + e.StackTrace);
    }
   finally
   {
       // close connection if it is still open

       // editing from phone so just writting the comments here
    }
}

Let me know of the outcome.

like image 62
TejjD Avatar answered Nov 15 '22 03:11

TejjD