<body> <form id="form1" runat="server"> <div> <%--three labels and three textboxes--%> <asp:Label ID="Label1" runat="server" Text="Roll" Width="60px"></asp:Label> <asp:TextBox ID="txtname" runat="server"></asp:TextBox><br /> <asp:Label ID="Label2" runat="server" Text="Name" Width="60px"></asp:Label> <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox><br /> <asp:Label ID="Label3" runat="server" Text="Marks" Width="60px"></asp:Label> <asp:TextBox ID="txtsal" runat="server"></asp:TextBox><br /> <%--one link button to insert record into database--%> <asp:LinkButton Text="Insert" ID="lnkbtn_insert" runat="server" onclick="lnkbtn_insert_Click" OnClientClick="return confirm('Are you sure want to insert this record?');" /> <%--Display data in the grid view--%> <asp:GridView ID="GridView1" runat="server"></asp:GridView> </div> </form> </body>
protected void Page_Load(object sender, EventArgs e) { } string strConnString = "Data Source=XXXXX; Initial Catalog=XXXXX; User Id=testLogin; Password=xxxx@1234"; string str; SqlCommand com; SqlDataAdapter sqlda; DataSet ds; protected void lnkbtn_insert_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(strConnString); con.Open(); com = new SqlCommand("insert into Student values(@Roll,@name,@Marks)", con); com.Parameters.AddWithValue("@Roll", txtname.Text); com.Parameters.AddWithValue("@name", txtaddress.Text); com.Parameters.AddWithValue("@Marks", txtsal.Text); com.ExecuteNonQuery(); com.Dispose(); con.Close(); Response.Write("Records successfully inserted"); clear(); bindgrid(); } void clear() { txtname.Text = ""; txtaddress.Text = ""; txtsal.Text = ""; } private void bindgrid() { SqlConnection con = new SqlConnection(strConnString); con.Open(); str = "select * from Student"; com = new SqlCommand(str, con); sqlda = new SqlDataAdapter(com); ds = new DataSet(); sqlda.Fill(ds, "student"); GridView1.DataMember = "student"; GridView1.DataSource = ds; GridView1.DataBind(); con.Close(); }
Mark This Response as Answer -- Chandu http://www.dotnetfunda.com/images/dnfmvp.gif
Login to post response