Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

UPDATE NOT CORRECT IN GRIDVIEW

$
0
0

when click update ,the date change textbox suchas :

I change  data in firstname column  when click update save data in phone column and click update svae in last name column

why do this ?

I sahre my code here :

--- class person property

Imports

system.data

Imports

System.data.sqlclient

Imports

Microsoft.win32

Public

Class Personpro

Private _ID As Integer

Private _phone As String

Private _FNAME As String

Private _LNAME As String

Private con As New SqlConnection

Public Sub getconnectioN()

Try

con =

New SqlConnection(DBCONNECTION.CONSTR)

con.Open()

Catch ex As Exception

MsgBox(

"CANN'T OPEN DATABASE")

Finally

con.Close()

End Try

End Sub

Public Sub New()

getconnectioN()

Dim cmd As New SqlCommand

Dim dr As SqlDataReader

Dim sql As String = " select Pers_PersonId ,Pers_FirstName , Pers_LastName,Pers_PhoneNumber from Person where 1 =1 "

With cmd

.CommandType = CommandType.Text

.CommandText = sql

.Connection = con

End With

con.Open()

dr = cmd.ExecuteReader

While dr.Read

_ID = Convert.ToString(dr(

"Pers_PersonId"))

_FNAME = Convert.ToString(dr(

"Pers_FirstName"))

_LNAME = Convert.ToString(dr(

"Pers_LastName"))

_phone = Convert.ToString(dr(

"Pers_PhoneNumber"))

End

While

End Sub

Public ReadOnly Property id() As Integer

Get

id = _ID

End Get

End Property

Public ReadOnly Property phone() As String

Get

phone = _phone

End Get

End Property

Public ReadOnly Property FNAME() As String

Get

FNAME = _FNAME

End Get

End Property

Public ReadOnly Property LNAME() As String

Get

LNAME = _LNAME

End Get

End Property

End Class

--- class methods update and delete

Imports

System.Data

Imports

System.Data.SqlClient

Imports

Microsoft.win32

Public

Class Person

Private con As New SqlConnection

Public Sub getconnectioN()

Try

con =

New SqlConnection(DBCONNECTION.CONSTR)

con.Open()

Catch ex As Exception

MsgBox(

"CANN'T OPEN DATABASE")

Finally

con.Close()

End Try

End Sub

Public Sub Updt(ByVal ID As Integer, ByVal phone As String, ByVal FNAME As String, ByVal LNAME As String)

getconnectioN()

Dim cmd As New SqlCommand

Dim da As New SqlDataAdapter

Dim sql As String = " update Person set Pers_PersonId = '" & ID & "', Pers_FirstName = '" & FNAME & "', Pers_LastName = '" & LNAME & "'," & _

"Pers_PhoneNumber = '" & phone & "'" & _

"Where Pers_PersonId = " & ID

Withcmd

.CommandType = CommandType.Text

.CommandText = sql

.Connection = con

End With

da.SelectCommand = cmd

con.Open()

cmd.ExecuteNonQuery()

con.Close()

End Sub

Public Sub delete(ByVal ID As Integer)

getconnectioN()

Dim cmd As New SqlCommand

Dim da As New SqlDataAdapter

Dim sql As String = " Delete from Person " & _

" where Pers_PersonId = " & ID

With cmd

.CommandType = CommandType.Text

.CommandText = sql

.Connection = con

 

End With

da.SelectCommand = cmd

con.Open()

cmd.ExecuteNonQuery()

con.Close()

End Sub

Public Function find() As DataSet

getconnectioN()

Dim cmd As New SqlCommand

Dim sql As String = " Select Pers_PersonId , Pers_FirstName , Pers_LastName , Pers_PhoneNumber from Person where 1 = 1 "

Dim dt As New DataSet

Dim da As New SqlDataAdapter

With cmd

.CommandType = CommandType.Text

.CommandText = sql

.Connection = con

End With

da.SelectCommand = cmd

da.Fill(dt)

Return dt

End Function

End Class

----  class events  form

 

Imports

System.Data.SqlClient

Imports

System.data

Imports

Microsoft.Win32

Partial

Class PersonCRM

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try

If Not Page.IsPostBack Then

Dim x As New Person

Dim dT As DataSet = x.find()

GVperson.DataSource = dT

GVperson.DataBind()

End If

Catch ex As Exception

MsgBox(ex.Message &

" ERROR Page_Load ")

End

Try

End Sub

Protected Sub GVperson_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GVperson.PageIndexChanging

GVperson.PageIndex = e.NewPageIndex

GVperson.DataBind()

Dim x As New Person

Dim DT AsDataSet = x.find()

GVperson.DataSource() = DT

GVperson.DataBind()

End Sub

Protected Sub GVperson_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GVperson.RowDataBound

Try

If e.Row.RowType = DataControlRowType.DataRow Then

Dim txtID As TextBox = CType(e.Row.FindControl("txtID"), TextBox)

Dim txtfname As TextBox = CType(e.Row.FindControl("txtfname"), TextBox)

Dim txtlname As TextBox = CType(e.Row.FindControl("txtlname"), TextBox)

Dim txtphone As TextBox = CType(e.Row.FindControl("txtphone"), TextBox)

Dim x As New Personpro

If x.id.ToString() = Nothing Then

txtID.Text = 0

End If

If x.FNAME.Trim() = Nothing Then

txtfname.Text = 0

End If

If x.LNAME.Trim() = Nothing Then

txtlname.Text = 0

End If

If x.phone.Trim() = Nothing Then

txtphone.Text = 0

End If

End If

Catch ex As Exception

MsgBox(ex.Message &

" ERROR RowDataBound")

End

Try

End Sub

Protected Sub GVPERSON_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GVperson.RowUpdating

Try

Dim row As GridViewRow = DirectCast(GVperson.Rows(e.RowIndex), GridViewRow)

Dim ID As Integer

Dim FNAME As String

Dim LNAME As String

Dim phone As String

Dim txt As TextBox

txt =

CType(row.FindControl("txtId"), TextBox)

ID = txt.Text.Trim()

txt =

CType(row.FindControl("txtfname"), TextBox)

FNAME = txt.Text.Trim()

txt =

CType(row.FindControl("txtlname"), TextBox)

LNAME = txt.Text.Trim()

txt =

CType(row.FindControl("txtphone"), TextBox)

phone = txt.Text.Trim()

Dim objper As New Person

objper.Updt(ID, FNAME, LNAME, phone)

Dim Dt As DataSet = objper.find()

GVperson.DataSource = Dt

GVperson.DataBind()

Catch ex As Exception

MsgBox(ex.Message &

" CAN NOT update DATA")

End Try

End Sub

Protected Sub GVPERSON_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GVperson.RowDeleting

Try

Dim row As GridViewRow = DirectCast(GVperson.Rows(e.RowIndex), GridViewRow)

Dim ID As Integer

Dim phone As String

Dim FNAME As String

Dim LNAME As String

Dim txt As TextBox

txt =

CType(row.FindControl("txtid"), TextBox)

ID = txt.Text.Trim()

txt =

CType(row.FindControl("txtfname"), TextBox)

FNAME = txt.Text.Trim()

txt =

CType(row.FindControl("txtlname"), TextBox)

LNAME = txt.Text.Trim()

txt =

CType(row.FindControl("txtphone"), TextBox)

phone = txt.Text.Trim()

Dim objper As New Person

objper.delete(ID)

Dim Dt AsDataSet = objper.find()

GVperson.DataSource = Dt

GVperson.DataBind()

Catch ex As Exception

MsgBox(ex.Message &

" CAN NOT delete DATA ")

End Try

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim x As New Person

Dim dt As DataSet = x.find()

GVperson.DataSource = dt

GVperson.DataBind()

End Sub

End Class


Viewing all articles
Browse latest Browse all 3509

Trending Articles