Reputation: 119
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();
}
}
Upvotes: 2
Views: 4829
Reputation: 5202
For that You need to check ViewState with older one.. There is an example I figured out and solved that issue. Now new data would not Inserted after the postback (on page refresh).
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack && !IsRefresh){
BindGrid();
}
}
private bool _isRefresh;
public bool IsRefresh
{
get { return _isRefresh; }
}
protected override void LoadViewState(object savedState)
{
object[] allStates = (object[])savedState;
base.LoadViewState(allStates[0]);
_refreshState = Convert.ToBoolean(allStates[1]);
_isRefresh = _refreshState == Convert.ToBoolean(Session["__ISREFRESH"]);
}
protected override object SaveViewState()
{
Session["__ISREFRESH"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();
allStates[1] = !_refreshState;
return allStates;
}
Thanks
Upvotes: 2
Reputation: 33809
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...
Upvotes: 3