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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Jul 17, 2010, 10:35 AM
ilaPosted Jul 17, 2010, 9:15 AM
Its datagridview
With regards
ila
gaurav bembiPosted Jul 15, 2010, 3:18 AM
@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
Satyapriya NayakPosted Jul 15, 2010, 2:58 AM
Plz run the attachments GridView Insert update delete.....
If this helps you plz mark it as Accepted Answer
Thanks
Sam HobbsPosted Jul 15, 2010, 2:08 AM
gaurav bembiPosted Jul 15, 2010, 2:05 AM
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------------------------
For delete pass just one parameter ok
Best of luck!!