Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:49:20+00:00 2026-05-31T17:49:20+00:00

I have a datagrid that I have filled with data from a sql database.

  • 0

I have a datagrid that I have filled with data from a sql database. I have also added a feature to add a new contact to the database which works well. The problem I’m having though is that after adding a contact and hitting F5 (refresh webpage) it adds another identical contact to the database.

I’m clearing the textfields after postback but somehow the string are left in the memory and adds another contact every time a refresh the webpage.

I also have the issue of the datagrid not updating instantly after clicking the button which is why I had to update the page in the first place. I believe these two issues may be related.

Here’s my code behind, I don’t think the aspx page will be necessary but I can give it if needed.

public partial class Default : System.Web.UI.Page
{
    SqlConnection connection = new SqlConnection("server = Sqlconnection; uid = username; pwd = password; database = database;");

    protected void Page_Init(object sender, EventArgs e)
    {
        //------------------------------------------------DataGrid--------------------------------------------------
        SqlDataAdapter SqlCommandDG = new SqlDataAdapter("SELECT FirstName, LastName, Email, PhoneNumber, CompanyName FROM ContactPerson CP, Company C WHERE CP.[CompanyID] = C.[Company_ID]", connection);

        DataSet ds = new DataSet();
        SqlCommandDG.Fill(ds);

        DataView source = new DataView(ds.Tables[0]);
        DataGrid1.DataSource = source;
        DataGrid1.DataBind();

        //----------------------------------------------Dropdown list------------------------------------------------
        SqlCommand SqlCommandDD = new SqlCommand("SELECT * FROM Company");
        SqlCommandDD.Connection = connection;
        connection.Open();

        DropDownList1.DataSource = SqlCommandDD.ExecuteReader();
        DropDownList1.DataValueField = "Company_ID";
        DropDownList1.DataTextField = "CompanyName";
        DropDownList1.DataBind();

        connection.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        connection.Open();

        string fName = fNameTextBox.Text;
        string lName = lNameTextBox.Text;
        string email = EmailTextBox.Text;
        string phoneNr = phoneNrTextBox.Text;
        string company = DropDownList1.SelectedValue;

        string sqlquery = ("INSERT INTO ContactPerson (FirstName, LastName, Email, PhoneNumber, CompanyID) VALUES ('" + fNameTextBox.Text + "','" + lNameTextBox.Text + "','" + EmailTextBox.Text + "','" + phoneNrTextBox.Text + "','" + DropDownList1.SelectedValue + "')");
        SqlCommand command = new SqlCommand(sqlquery, connection);

        command.Parameters.AddWithValue("FirstName", fName);
        command.Parameters.AddWithValue("LastName", lName);
        command.Parameters.AddWithValue("Email", email);
        command.Parameters.AddWithValue("PhoneNumber", phoneNr);
        command.Parameters.AddWithValue("CompanyID", company);
        command.ExecuteNonQuery();

        fNameTextBox.Text = string.Empty;
        lNameTextBox.Text = string.Empty;
        EmailTextBox.Text = string.Empty;
        phoneNrTextBox.Text = string.Empty;

        connection.Close();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:49:21+00:00Added an answer on May 31, 2026 at 5:49 pm

    If you press F5 it will repeat your last request which is adding a record to the db in this case. You have to check with the db for the existence of the record before inserting it to avoid this.

    Secondly, If you need your datagrid to be updated with last entered value you need to re-bind the datagrid after entering the record. Remove the databinding code from Page_Init method and create a new method with same code say BindGrid() and do the following

    protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostBack){
         BindGrid();
       }
    }
    

    Also, add BindGrid(); as the last line of Button1_Click() method (ie; after entering the record)

    Hope that helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagrid which populated by some data from sqlserver.I have datetime filed
I have a DataGrid that is loaded from an XML data store, all created
I'm fairly new to Silverlight. I have an AutoCompleteBox which is filled with Data
I have a Datagrid that gets its data from an ArrayCollection of model beans.
I have a DataGrid that is automatically fed certain values from an LINQ-To-SQL-Source. The
I have a datagrid that I want to add a button/s to at runtime.
I have a DataGrid that is showing some data via a PagedCollectionView with one
I have a DataGrid component that displays a few columns of data. It has
I have a Silverlight DataGrid that contains a single template column which displays a
I have a Silverlight DataGrid that's being populated with different types of data for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.