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();
}
}
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
Also, add BindGrid(); as the last line of Button1_Click() method (ie; after entering the record)
Hope that helps…