Update gridview editable changes to dataset inturn to a sql table
Here is my .aspx page
Now i am binding it from code behind on page load as given below
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim str As String = ConfigurationManager.AppSettings("DBConn")
Dim Conn As New SqlConnection(str)
Conn.Open()
Dim qry As String = "SELECT * FROM dbo.Employee"
Dim adp As New SqlDataAdapter(qry, Conn)
Dim ds As New DataSet
adp.Fill(ds)
GV.DataSource = ds
GV.DataBind()
End If
End Sub
Now I have textbox as a editable field and user will make the changes to those textboxes
and What i want is after click of a save button those changes to be updated in dataset and further in sql Employee table
Without any kind of loop
Please suggest
Satyapriya NayakPosted Nov 12, 2012, 11:46 PM
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication7._Default" %>
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class _Default
Inherits System.Web.UI.Page
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("ConnectionString").ToString()
Dim con As New SqlConnection(strConnString)
Dim str As String
Dim com As SqlCommand
Dim sqlda As SqlDataAdapter
Dim ds As DataSet
Protected Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
Dim strSql As New StringBuilder(String.Empty)
Dim cmd As New SqlCommand()
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim chkUpdate As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("chkSelect"), CheckBox)
If chkUpdate IsNot Nothing Then
If chkUpdate.Checked Then
Dim strID As String = GridView1.Rows(i).Cells(1).Text
Dim strName As String = DirectCast(GridView1.Rows(i).FindControl("txtName"), TextBox).Text
Dim strLocation As String = DirectCast(GridView1.Rows(i).FindControl("txtEmail"), TextBox).Text
Dim strUpdate As String = "Update Details set Name = '" & strName & "',Email = '" & strLocation & "' WHERE ID ='" & strID & "'"
strSql.Append(strUpdate)
End If
End If
Next
Try
cmd.CommandType = CommandType.Text
cmd.CommandText = strSql.ToString()
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
Catch ex As SqlException
Dim errorMsg As String = "Error in Updation"
errorMsg += ex.Message
Throw New Exception(errorMsg)
Finally
con.Close()
End Try
UncheckAll()
End Sub
Private Sub UncheckAll()
For Each row As GridViewRow In GridView1.Rows
Dim chkUncheck As CheckBox = DirectCast(row.FindControl("chkSelect"), CheckBox)
Dim txtname As TextBox = DirectCast(row.FindControl("txtName"), TextBox)
Dim txtEmail As TextBox = DirectCast(row.FindControl("txtEmail"), TextBox)
chkUncheck.Checked = False
txtname.[ReadOnly] = True
txtEmail.[ReadOnly] = True
txtname.ForeColor = System.Drawing.Color.Blue
txtEmail.ForeColor = System.Drawing.Color.Blue
Next
End Sub
Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim chkTest As CheckBox = DirectCast(sender, CheckBox)
Dim grdRow As GridViewRow = DirectCast(chkTest.NamingContainer, GridViewRow)
Dim txtname As TextBox = DirectCast(grdRow.FindControl("txtName"), TextBox)
Dim txtEmail As TextBox = DirectCast(grdRow.FindControl("txtEmail"), TextBox)
If chkTest.Checked Then
txtname.[ReadOnly] = False
txtEmail.[ReadOnly] = False
txtname.ForeColor = System.Drawing.Color.Black
txtEmail.ForeColor = System.Drawing.Color.Black
Else
txtname.[ReadOnly] = True
txtEmail.[ReadOnly] = True
txtname.ForeColor = System.Drawing.Color.Blue
txtEmail.ForeColor = System.Drawing.Color.Blue
End If
End Sub
End Class
Thanks
If this post helps you mark it as answer