0

HI im new to asp.net and i want help when click insert button same record is inserted instead of new record. Pls reply asap as my project is based on this

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    public System.Data.SqlClient.SqlConnection conn;
    public System.Data.SqlClient.SqlDataAdapter adap;
    public System.Data.SqlClient.SqlDataReader rd;
    public System.Data.SqlClient.SqlCommand cmd;
    DataSet ds;
    int rno=0;    
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection();
        conn.ConnectionString = "Data Source=PC-4\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
        loaddata();
        showdata();
    }
    void loaddata()
    {
        adap = new SqlDataAdapter("select * from employees", conn);
        ds = new DataSet();
        adap.Fill(ds, "employees");
        ds.Tables[0].Constraints.Add("pk_eid", ds.Tables[0].Columns[0], true);//Creating primary key for tables in dataset
        //rd = new SqlDataReader();
       //rd = cmd.ExecuteReader();
    }

    void showdata()
    {
        TextBox1.Text = ds.Tables[0].Rows[rno][0].ToString();
        TextBox2.Text = ds.Tables[0].Rows[rno][1].ToString();
        TextBox3.Text = ds.Tables[0].Rows[rno][2].ToString();
        TextBox4.Text = ds.Tables[0].Rows[rno][3].ToString();
    }

    protected void insert_Click(object sender, EventArgs e)
    {
        
            
            String s;
            s = "insert into employees values(" +TextBox1.Text+ ",'" +TextBox2.Text+ "','" +TextBox3.Text+ "','" + TextBox4.Text+ "')" ;
            cmd = new SqlCommand(s,conn);
            conn.Open(); 
            int n = cmd.ExecuteNonQuery();
            conn.Close();
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
    }
}

Edited by __avd: Added [code] tags.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by __avd
0

You need to use IsPostBack property.

protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection();
        conn.ConnectionString = "Data Source=PC-4\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
      if(!IsPostBack) 
       {
        loaddata();
        showdata();
       }
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.