Hi to all
I need the sample code to perform insert, delete, update the values in database using grid view.. Its urgent
Thanks in advance
With regards
ila
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
gaurav bembi41 reputationPosted Jul 14, 2010, 10:05 PM EDT
try this to insert and update the data.............
VB.NET-------------
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Dim con As New SqlConnection()
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As New SqlCommand()
con.ConnectionString = ConfigurationManager.ConnectionStrings("cn").ConnectionString
'open connection if it is closed
If con.State = ConnectionState.Closed Then
con.Open()
End If
''''''''''''''''''''''''''''''''''''''''''''
cmd.Connection = con
cmd.CommandText = "insert into tbemp values(@ID,@Name,@Address,@Salary)"
'Pass parameters
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(txtID.Text)
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtName.Text
cmd.Parameters.Add("@Salary", SqlDbType.Int).Value = Convert.ToInt32(txtSalary.Text)
cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = txtAddress.Text
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
ClearRecord()
End Sub
Public Sub ClearRecord()
txtID.Text = String.Empty
txtName.Text = String.Empty
txtAddress.Text = String.Empty
txtSalary.Text = String.Empty
txtID.Focus()
GridBind()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cmd As New SqlCommand()
con.ConnectionString = ConfigurationManager.ConnectionStrings("cn").ConnectionString
If con.State = ConnectionState.Closed Then
con.Open()
End If
cmd.Connection = con
cmd.CommandText = "update tbemp set Name=@Name,Address=@Address,Salary=@Salary where ID=@ID"
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(txtID.Text)
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtName.Text
cmd.Parameters.Add("@Salary", SqlDbType.Int).Value = Convert.ToInt32(txtSalary.Text)
cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = txtAddress.Text
cmd.ExecuteNonQuery()
cmd.Dispose()
con.Close()
ClearRecord()
End Sub
Public Sub GridBind()
Dim adp As New SqlDataAdapter("select * from tbemp", ConfigurationManager.ConnectionStrings("cn").ConnectionString)
Dim ds As New DataSet()
adp.Fill(ds)
GridView1.DataSource = ds
GridView1.DataBind()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
GridBind()
End If
End Sub
End Class
--------------------------------------------------------------------------------------
C#.NET------------------------
using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Data.SqlClient; using System.Configuration; partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection(); protected void Button1_Click(object sender, System.EventArgs e) { SqlCommand cmd = new SqlCommand(); con.ConnectionString = ConfigurationManager.ConnectionStrings("cn").ConnectionString; //open connection if it is closed if (con.State == ConnectionState.Closed) { con.Open(); } ///''''''''''''''''''''''''''''''''''''''''' cmd.Connection = con; cmd.CommandText = "insert into tbemp values(@ID,@Name,@Address,@Salary)"; //Pass parameters cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(txtID.Text); cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtName.Text; cmd.Parameters.Add("@Salary", SqlDbType.Int).Value = Convert.ToInt32(txtSalary.Text); cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = txtAddress.Text; cmd.ExecuteNonQuery(); cmd.Dispose(); con.Close(); ClearRecord(); } public void ClearRecord() { txtID.Text = string.Empty; txtName.Text = string.Empty; txtAddress.Text = string.Empty; txtSalary.Text = string.Empty; txtID.Focus(); GridBind(); } protected void Button2_Click(object sender, System.EventArgs e) { SqlCommand cmd = new SqlCommand(); con.ConnectionString = ConfigurationManager.ConnectionStrings("cn").ConnectionString; if (con.State == ConnectionState.Closed) { con.Open(); } cmd.Connection = con; cmd.CommandText = "update tbemp set Name=@Name,Address=@Address,Salary=@Salary where ID=@ID"; cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(txtID.Text); cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = txtName.Text; cmd.Parameters.Add("@Salary", SqlDbType.Int).Value = Convert.ToInt32(txtSalary.Text); cmd.Parameters.Add("@Address", SqlDbType.VarChar, 50).Value = txtAddress.Text; cmd.ExecuteNonQuery(); cmd.Dispose(); con.Close(); ClearRecord(); } public void GridBind() { SqlDataAdapter adp = new SqlDataAdapter("select * from tbemp", ConfigurationManager.ConnectionStrings("cn").ConnectionString); DataSet ds = new DataSet(); adp.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } protected void Page_Load(object sender, System.EventArgs e) { if (Page.IsPostBack == false) { GridBind(); } } public _Default() { Load += Page_Load; } }For delete pass just one parameter ok
Best of luck!!
Sam HobbsRank #107 · 20.5K reputationPosted Jul 14, 2010, 10:08 PM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Jul 14, 2010, 10:58 PM EDT
Plz run the attachments GridView Insert update delete.....
If this helps you plz mark it as Accepted Answer
Thanks
gaurav bembi41 reputationPosted Jul 14, 2010, 11:18 PM EDT
@satyapriya nayak
I hv checked ur attachment
its gud
u know u r using inline queries
Sql injection can be easily applied on it
so try to avoid inline query and use store procedure fine!!
Take care
and best of luck
ila41 reputationPosted Jul 17, 2010, 5:15 AM EDT
Its datagridview
With regards
ila
Sam HobbsRank #107 · 20.5K reputationPosted Jul 17, 2010, 6:35 AM EDT