ASP.NET - Problem with Insert Operation - Asked By Ajay Paritala on 29-Nov-11 03:00 AM

Hi,

I performed Insert operation and it must display result in a GridView. It works perfect when i press insert Button But,
 1: After record get inserted Grid View is not getting display the updated record
2: If i Refresh the browser then same product getting inserted again and again.
Jitendra Faye replied to Ajay Paritala on 29-Nov-11 03:11 AM

After insert record bind your GridView again,

like this-


protected


void BtnInsert_Click(object sender, EventArgs e)

{

  //Insert code
//Rebind your GridView.
}

Try this and let em know.

dipa ahuja replied to Ajay Paritala on 29-Nov-11 03:17 AM
Take a AddButton inside the foolter template and try this way to insert record and also rebind it after the insertion

Step 1 : Add new template Field With the Required Controls:
 
<asp:TemplateField>
<FooterTemplate>
  Name:<asp:TextBox ID="txtname" runat="server" /><br />
  Gender :
  <asp:DropDownList ID="ddGender" runat="server">
  <asp:ListItem>Male</asp:ListItem>
  <asp:ListItem>Female</asp:ListItem>
  </asp:DropDownList>
      
  <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
</FooterTemplate>
</asp:TemplateField>
   
► Step 2 Implement the Button's Click event:
protected void btnInsert_Click(object sender, EventArgs e)
{
  string name = ((TextBox)GridView1.FooterRow.FindControl("txtname")).Text;
  string gender = ((DropDownList)GridView1.FooterRow.FindControl("ddGender")).SelectedValue;
 
  string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
  SqlConnection conn = new SqlConnection(ConnString);
  
  conn.Open();
  
  string q = "insert into people (name,gender) values(@name,@gender)";
  SqlCommand comm = new SqlCommand(q,conn);
 
  comm.Parameters.AddWithValue("name", name);
  comm.Parameters.AddWithValue("gender", gender);
  comm.ExecuteNonQuery();
  conn.Close();
 
  BindGrid(); //Re-bind Gridview
}
 

Reena Jain replied to Ajay Paritala on 29-Nov-11 03:17 AM
hi,

Filling DataSet and binding to GridView is right, but might be what you are doing is wrong if you have written Insert query and so insert query is never gonna return you a result set no matter what you write,so make seprate INSERT METHOD where you will fire insert query and one more method which will select data from data base i.e SELECT query and bind your gridview inside that method

you can not bind grid view with INSERT stmt
Riley K replied to Ajay Paritala on 29-Nov-11 03:19 AM

Page Refresh will cause insertion of duplicate records in database

You can handle this in two ways

After  insert operation just redirect to that page

protected void BtnSubmit_Click(object sender, EventArgs e)
{

//code for insert

 Response.Redirect(Request.Url.ToString(), false)

}


Another way is to check for duplicate exists before insert

You can do with a stored procedure

IF EXISTS (SELECT * FROM Bookings WHERE FLightID = @Id)
BEGIN
  --UPDATE HERE
END
ELSE
BEGIN
   -- INSERT HERE
END

Regards
Ajay Paritala replied to Jitendra Faye on 29-Nov-11 03:43 AM
Ya! Am doing in the same way

Insert method();
Bind Method();