title: Save button click event getting fired up on refreshing the page once done with binding the data to a gridview through save button in asp.net
body: <p>I am working on a web application using C# and ASP.NET. I am binding the data to the gridview through the textboxes in the same page and I wrote the binding method in my "Save" button click event. </p>
  
  <p>Now, it's really strange thing to find that the grid view is getting bound again with duplicate rows once I refresh the page up on saving the data to the gridview from textboxes through "save" <code>button_click</code> event. I have tried loading the page on firefox, chrome and IE 8....but the result is negative....</p>
  
  <p>Could anyone let me know why is it happening and guide me to resolve it....</p>
  
  <p>This is my C# code:</p>
  
  <pre><code>string con = ConfigurationSettings.AppSettings["ConnectionStrings"];
  </code></pre>
  
  <p>protected void Page_Load(object sender, EventArgs e)
      {</p>
  
  <pre><code>    tbladdasset.Visible = false;
      btnsaveasset.Enabled = false;
      lblErrMsg.Text = "";
  
      if (!IsPostBack)
      {
          bindassets("", "");
          ViewState["sortOrder"] = "";
      }
  }
  
  private void bindassets(string sortExp, string sortDir)
  {
      try
      {
          SqlConnection con1 = new SqlConnection(con);
          con1.Open();
          SqlCommand cmd = new SqlCommand("select Description,CONVERT(VARCHAR(10), RecievedDate, 101) as DateRecieved,cost,Modelno,Quantity from Asset", con1);
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          da.Fill(dt);
          con1.Close();
  
          if (dt.Rows.Count &gt; 0)
          {
              DataView dv = dt.DefaultView;
  
              if (sortExp != string.Empty)
              {
                  dv.Sort = string.Format("{0} {1}", sortExp, sortDir);
              }
  
              grdvAssets.DataSource = dv;
              grdvAssets.DataBind();
  
          }
          else
          {
              lblErrMsg.Text = "No data found...";
          }
  
          btnsaveasset.Enabled = false;
          tbladdasset.Visible = false;
      }
      catch
      {
          lblErrMsg.Text = "Failed to connect server...";
      }
  }
  
  protected void btnaddnew_Click(object sender, EventArgs e)
  {
      tbladdasset.Visible = true;
      btnsaveasset.Enabled = true;
      lblErrMsg.Text = "";
      txtdescription.Text = "";
      txtdtrecieved.Text = "";
      txtcost.Text = "";
      txtmodelno.Text = "";
      txtquantity.Text = "";
  }
  
  protected void btnsaveasset_Click(object sender, EventArgs e)
  {
          if (txtdescription.Text != "" &amp;&amp; txtdtrecieved.Text != "" &amp;&amp; txtcost.Text != "" &amp;&amp; txtmodelno.Text != "" &amp;&amp; txtquantity.Text != "")
          {
              try
              {
                  string desc= txtdescription.Text;
                  DateTime dtrecd = Convert.ToDateTime(txtdtrecieved.Text);
                  string cost = txtcost.Text;
                  string modelno = txtmodelno.Text;
                  double quantity = Convert.ToDouble(txtquantity.Text);
                  SqlConnection sqlcon = new SqlConnection(con);
                  sqlcon.Open();
                  string save = "Insert into Asset(Description,Recieveddate,cost,Modelno,Quantity)values(@desc,@date,@cost,@modelno,@quantity)";
                  SqlCommand cmd = new SqlCommand(save, sqlcon);
                  cmd.CommandType = CommandType.Text;
                  cmd.Parameters.Add("@desc", desc);
                  cmd.Parameters.Add("@date", dtrecd);
                  cmd.Parameters.Add("@cost", cost);
                  cmd.Parameters.Add("@modelno", modelno);
                  cmd.Parameters.Add("@quantity", quantity);
                  cmd.ExecuteNonQuery();
                  sqlcon.Close();
                  bindassets("", "");
                  btnsaveasset.Enabled = false;
                  txtdescription.Text = "";
                  txtdtrecieved.Text = "";
                  txtcost.Text = "";
                  txtmodelno.Text = "";
                  txtquantity.Text = "";
                  lblErrMsg.Text = "data inserted successfully..";
              }
              catch
              {
                  lblErrMsg.Text = "Please enter valid data and try again...";
              }
          }
          else
          {
              lblErrMsg.Text = "Please enter valid data and try again...";
          }
  
  }
  
  protected void grdvAssets_Sorting(object sender, GridViewSortEventArgs e)
  {
      bindassets(e.SortExpression, sortOrder);
  }
  
  public string sortOrder
  {
      get
      {
          if (ViewState["sortOrder"].ToString() == "desc")
          {
              ViewState["sortOrder"] = "asc";
          }
          else
          {
              ViewState["sortOrder"] = "desc";
          }
  
          return ViewState["sortOrder"].ToString();
      }
      set
      {
          ViewState["sortOrder"] = value;
      }
  }
  </code></pre>
  
  <p>Anyone please help me.....Thanks in advance..</p>