Berchev
Berchev

Reputation: 87

ASP.NET C# update query not working

I am working on one project for the university and I want to say that I am pretty new in asp.net.

Please find below the code that I am experiencing problems with. The problem is that the update function is not working. In the page load I have some select queries and I load a data from the database into some textareas and textfields. This is working fine - it loads the sample data that I have added manually in my database.

I have buttons that should update the database on click.

This is the code for the buttons:

  <a id="A1"  class="button" onserverclick="box1_Click" runat="server">
        <span>Запази полето <img src="notification-tick.gif" width="12" height="12" /></span>
        </a>

This is the code behind:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Loading the data from the database

        string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString;
        string sql1_title = "SELECT title FROM home WHERE id=1";
        string sql1_image = "SELECT image FROM home WHERE id=1";
        string sql1_text = "SELECT text FROM home WHERE id=1";

        string sql2_title = "SELECT title FROM home WHERE id=2";
        string sql2_image = "SELECT image FROM home WHERE id=2";
        string sql2_text = "SELECT text FROM home WHERE id=2";

        string sql3_title = "SELECT title FROM home WHERE id=3";
        string sql3_image = "SELECT image FROM home WHERE id=3";
        string sql3_text = "SELECT text FROM home WHERE id=3";
        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            //box1 data load
            SqlCommand cmd1_title = new SqlCommand(sql1_title, conn);
            conn.Open();
            box1_title.Text = (string)cmd1_title.ExecuteScalar();
            conn.Close();
            SqlCommand cmd1_image = new SqlCommand(sql1_image, conn);
            conn.Open();
            box1_img.Text = (string)cmd1_image.ExecuteScalar();
            conn.Close();
            SqlCommand cmd1_text = new SqlCommand(sql1_text, conn);
            conn.Open();
            box1_text.InnerText = (string)cmd1_text.ExecuteScalar();
            conn.Close();

            //box2 data load
            SqlCommand cmd2_title = new SqlCommand(sql2_title, conn);
            conn.Open();
            box2_title.Text = (string)cmd2_title.ExecuteScalar();
            conn.Close();
            SqlCommand cmd2_image = new SqlCommand(sql2_image, conn);
            conn.Open();
            box2_img.Text = (string)cmd2_image.ExecuteScalar();
            conn.Close();
            SqlCommand cmd2_text = new SqlCommand(sql2_text, conn);
            conn.Open();
            box2_text.InnerText = (string)cmd2_text.ExecuteScalar();
            conn.Close();

            //box3 data load
            SqlCommand cmd3_title = new SqlCommand(sql3_title, conn);
            conn.Open();
            box3_title.Text = (string)cmd3_title.ExecuteScalar();
            conn.Close();
            SqlCommand cmd3_image = new SqlCommand(sql3_image, conn);
            conn.Open();
            box3_img.Text = (string)cmd3_image.ExecuteScalar();
            conn.Close();
            SqlCommand cmd3_text = new SqlCommand(sql3_text, conn);
            conn.Open();
            box3_text.InnerText = (string)cmd3_text.ExecuteScalar();
            conn.Close();
        }
    }

    protected void box1_Click(object sender, EventArgs e)
    {
        string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString;
        string sql1 = "UPDATE home SET title=@title, image=@image, text=@text WHERE Id=1";
        using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
        {
            SqlCommand cmd2 = new SqlCommand(sql1, conn);

            cmd2.Parameters.AddWithValue("@title", box1_title.Text);
            cmd2.Parameters.AddWithValue("@image", box1_img.Text);
            cmd2.Parameters.AddWithValue("@text", box1_text.InnerText);

            conn.Open();
            cmd2.ExecuteNonQuery();
            conn.Close();
        }
    }

    protected void box2_Click(object sender, EventArgs e)
    {

    }


    protected void box3_Click(object sender, EventArgs e)
    {

    }
}

When I make a change to the title of the box1 and then click the button to update the database it is actually refreshing the page and loading the sample data again and my change is not saved.

Could you please help me with this?. There is no errors nothing.

Thank you guys very much.

PS: I have noticed that when I load the page in my browser and then delete the whole code block for the data load, make a change in the browser in one of the fields and then press the button it s actually updating the database. It is very strange...

Upvotes: 1

Views: 3980

Answers (2)

DatRid
DatRid

Reputation: 1169

This is because you need to take care about the ASP.NET lifecycle.

You need to check if its a PostBack (A postback occurs when you click the button) or not ... Or you will allways override your data.

 protected void Page_Load(object sender, EventArgs e)
 {
     if (!PostBack)
     {
          string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString;
          string sql = "SELECT id,title,image,text FROM home WHERE id in (1,2,3) order by id";    
          using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
          {
          conn.Open();

search for datareader -> and use it here ... Save the returning data into the datareader and filter by ID and if its item/title etc like here and here

          conn.Close();
          }

     }    
 }

Upvotes: 4

robert
robert

Reputation: 639

Fisrt, I want to suggest that, in the Page_Load, you only open the sql connection once and execute all the commands while the connection is open and then close it at the end. This will make your page load faster.

Second, you could use asp linkbutton to create a postback when clicked. The syntax should look like this:

<asp:LinkButton ID="A1" runat="server" OnClick="box1_Click">
    <span>Запази полето <img src="notification-tick.gif" width="12" height="12" /></span>
</asp:LinkButton>

Upvotes: 0

Related Questions