Keep (Retain) control values on page refresh (load) using C# in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

Hello Forum,

I recently created a webform where user can create receipt but I encountered some issues. The first issue is that when I click on “create receipt” button, the units in the wallet does not refresh and show the correct balance, it will still show the units the was there before clicking the “create receipt” button. Until I refresh the page with the browser button or navigate to another page; that is when the units in the wallet will show the correct balance. Secondly, the “total amount” in the editable side is reduced and not the same as the one in the side that is displaying the finished receipt for printing. The third one is that the last value in the Amount column of the gridview disappears when I click on the “create receipt” button.

Issue to solve:

  1. Automatically refresh page when the “create receipt” button is clicked, without affecting the values in the gridview and other label values in the editable side which is gridview1.
  1. When I refresh the page through the browser button, there is a deduction in the wallet unit. I want everything to stay the same when the browser button is used to refresh web page.

THIS ARE THE THINGS I WANT TO SOLVE ON THE WEB PAGE

CODE

public partial class CreateReceipt : System.Web.UI.Page
{
    SqlCommand cmd = new SqlCommand();
    SqlCommand cmd2 = new SqlCommand();
    SqlDataAdapter sda = new SqlDataAdapter();
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Response.Redirect("LoginForm.aspx");
        }
        else
        {
            showdata1();
            showdata();
            user.Visible = true;
            named.Visible = true;
            labelid.Visible = false;
            createby.Visible = false;
            role.Visible = false;
            showdata2();
        }
        string numbers = "1234567890";

        string characters = numbers;
        int length = 7;
        string id = string.Empty;
        for (int i = 0; i < length; i++)
        {
            string character = string.Empty;
            do
            {
                int index = new Random().Next(0, characters.Length);
                character = characters.ToCharArray()[index].ToString();
            } while (id.IndexOf(character) != -1);
            id += character;
        }
        Labelreceipt.Text = id;

        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
    public void showdata1()
    {
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "SELECT * FROM Users WHERE Uid = '" + Session["user"] + "'";
        cmd.Connection = con;
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        sda.SelectCommand = cmd;
        sda.Fill(ds, "detail");
        if (ds.Tables[0].Rows.Count > 0)
        {
            user.Text = ds.Tables[0].Rows[0]["email"].ToString();
            labelid.Text = ds.Tables[0].Rows[0]["Uid"].ToString();
            role.Text = ds.Tables[0].Rows[0]["Role"].ToString();
            createby.Text = ds.Tables[0].Rows[0]["CreatedBy"].ToString();
            securelbl.Text = ds.Tables[0].Rows[0]["Name"].ToString();
            OrganName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
        }
    }
    public void showdata()
    {
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "SELECT * FROM UserWallet AS w INNER JOIN Users AS u ON w.email = u.CreatedBy WHERE u.Uid = '" + Session["user"] + "'";
        cmd.Connection = con;
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        sda.SelectCommand = cmd;
        sda.Fill(ds, "detail");
        if (ds.Tables[0].Rows.Count > 0)
        {
            named.Text = ds.Tables[0].Rows[0]["Name"].ToString();
            Units.Text = ds.Tables[0].Rows[0]["amount"].ToString();
            securelbl.Text = ds.Tables[0].Rows[0]["Name"].ToString();
        }
    }
    public void showdata2()
    {
        cmd.CommandText = "select * from Users where Uid= '" + Session["user"] + "'";
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(ds, "detail");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string Role;
            Role = ds.Tables[0].Rows[0][3].ToString().Trim();

            if (Role == "User")
            {
                lipaybutton.Visible = false;
                dashboard.Visible = false;
                Li1.Visible = false;
                // buttoninvite.Visible = false;
            }
            else if (Role == "Admin")
            {
                lipaybutton.Visible = true;
                dashboard.Visible = true;
                Li1.Visible = true;
                //  buttoninvite.Visible = true;
            }
        }
    }
    private void Showreceipt()
    {
        DataTable dt = new DataTable();  
          using (SqlCommand cmd = new SqlCommand("SELECT items,qty,rate,amount FROM ReceiptTbl WHERE (receipt_no = '" + Session["Receipt"] + "')", con))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    con.Open();
                    cmd.CommandType = CommandType.Text;
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);

                    DataTable dt1 = dt.Clone();
                    foreach (DataRow dr in dt.Rows)
                    {
                        for (int i = 0; i < dr["items"].ToString().Split(',').Length; i++)
                        {
                            if (!string.IsNullOrEmpty(dr["items"].ToString().Split(',')[i]))
                            {
                                DataRow dr1 = dt1.NewRow();
                                dr1["items"] = dr["items"].ToString().Split(',')[i];
                                dr1["qty"] = dr["qty"].ToString().Split(',')[i];
                                dr1["rate"] = dr["rate"].ToString().Split(',')[i];
                                dr1["amount"] = dr["amount"].ToString().Split(',')[i];
                                dt1.Rows.Add(dr1.ItemArray);
                            }
                        }
                        Gridview2.DataSource = dt1;
                        Gridview2.DataBind();
                    }
                    con.Close();
                }
            }
        DisplayData();
    }
    private void DisplayData()
    {
        int id = Convert.ToInt32(Session["Id"]);

        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "SELECT * FROM ReceiptTbl WHERE receipt_no = '" + Session["Receipt"] + "'";
        cmd.Connection = con;
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        sda.SelectCommand = cmd;
        sda.Fill(ds, "detail");
        if (ds.Tables[0].Rows.Count > 0)
        {
            Orgname.Text = ds.Tables[0].Rows[0]["sname"].ToString();
            orgaddress.Text = ds.Tables[0].Rows[0]["saddress"].ToString();
            lblphone.Text = ds.Tables[0].Rows[0]["sphone"].ToString();

            reference.Text = ds.Tables[0].Rows[0]["receipt_no"].ToString();

            client.Text = ds.Tables[0].Rows[0]["rname"].ToString();
            clientaddrs.Text = ds.Tables[0].Rows[0]["raddress"].ToString();
            clientmail.Text = ds.Tables[0].Rows[0]["rmail"].ToString();
            clientmobile.Text = ds.Tables[0].Rows[0]["rphone"].ToString();
            lbldate.Text = ds.Tables[0].Rows[0]["dated"].ToString();

            totallbl.Text = ds.Tables[0].Rows[0]["total"].ToString();
            lblvat.Text = ds.Tables[0].Rows[0]["vat"].ToString();
            amountlbl.Text = ds.Tables[0].Rows[0]["due"].ToString();
        }
        byte[] QRBytes = GetQRCodeBytes(Server.MapPath("/CreateReceipt.aspx") + "?Id=" + id);
        Image2.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
    }
    public byte[] GetQRCodeBytes(string url)
    {
        QRCodeEncoder encoder = new QRCodeEncoder();
        Bitmap bi = encoder.Encode(url);
        MemoryStream tmpSteam = new MemoryStream();
        bi.Save(tmpSteam, ImageFormat.Jpeg);
        return tmpSteam.ToArray();
    }
}

 

Posted one month ago
George616 says:
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Response.Redirect("LoginForm.aspx");
        }
        else
        {
            showdata1();
            showdata();
            user.Visible = true;
            named.Visible = true;
            labelid.Visible = false;
            createby.Visible = false;
            role.Visible = false;
            showdata2();
        }

Verify the page load else condition that will be used to bind the labels inside the methods.

Rest code is ok.