Update Datagrid Question

J

Jennifer

I've got a datagrid. I want to allow the user to edit in the grid
directly. I'm using ASP with VB as the code behind in Visual Studio
2002.

I've never had a need or desire to do this before, so I found a
Quickstart example at DotNetJunkies.com. I've modified it for my needs

and can't get it to work. Specifically, the part where I get the
edited cell values does not work. e.Item.Cells(1).Text returns
nothing. None of the other indexes returns anything either. Please
help me out.


Truly appreciated,
Jennifer


Private Sub dgNotes_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgNotes.UpdateCommand
Dim sPicked As String
Dim i As Integer
Dim FName As String
Dim LName As String
Dim N As String
Dim sUpdateQuery As String
Dim con As New ConnectDB()
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim RetVal As Int16

'******
'** THIS BIT BELOW IS NOT WORKING PROPERLY
'** NO VALUES ARE BEING RETURNED FROM THE CELLS

Dim sFeedback As String = e.Item.Cells(1).Text
Dim sProject As String = e.Item.Cells(2).Text
Dim sShift As Integer = e.Item.Cells(3).Text
Dim sDate As String = e.Item.Cells(4).Text
Dim sNote As String = e.Item.Cells(5).Text
'*******

sPicked = Request("N")
i = InStr(sPicked, ",")
FName = Trim(Mid(sPicked, i + 1))
LName = Trim(Left(sPicked, i - 1))
N = Me.User.Identity.Name.ToString


sUpdateQuery = "UPDATE Captured_Data " & _
"Set Feedback = '" & sFeedback & "', Project = '" & _
sProject & "', Shift = " & _
sShift & ", [Date] = '" & Date.Now & "',Information = '" & _
sNote & "', " & _
"Agent_FirstName = '" & FName & "', Agent_LastName = '" & _
LName & "', Note_Author = '" & _
N & "' Where Agent_LastName = '" & LName & _
"' and Agent_FirstName = '" & FName & "' and " & _
"[Date] = '" & sDate & "'"

txtNotes.Text = sUpdateQuery

cn = con.ConnectDB
cn.Open()
cmd = New SqlClient.SqlCommand(sUpdateQuery, cn)
cmd.CommandType = CommandType.Text
RetVal = cmd.ExecuteNonQuery()
cn.Close()
FillGrid()
txtFeedback.Text = ""
txtProject.Text = ""
txtShift.Text = ""
txtNotes.Text = ""
lblErr.Visible = False


End Sub
 
N

neilmcguigan

hi jennifer

e.Item.Cells(n) actually refers to the table cell, and not the control
inside the table cell

use

dim tb as TextBox = CType(e.Item.Cells(n).Controls(0), TextBox)
Response.Write(tb.Text)

also, don't use dynamic sql. use parameters instead

cheers

neil
 
J

Jennifer

Hi Neil. Thanks for answering. It works now!! :) Yea! Thank you!!






hi jennifer

e.Item.Cells(n) actually refers to the table cell, and not the control
inside the table cell

use

dim tb as TextBox = CType(e.Item.Cells(n).Controls(0), TextBox)
Response.Write(tb.Text)

also, don't use dynamic sql. use parameters instead

cheers

neil
I've got a datagrid. I want to allow the user to edit in the grid
directly. I'm using ASP with VB as the code behind in Visual Studio
2002.

I've never had a need or desire to do this before, so I found a
Quickstart example at DotNetJunkies.com. I've modified it for my needs

and can't get it to work. Specifically, the part where I get the
edited cell values does not work. e.Item.Cells(1).Text returns
nothing. None of the other indexes returns anything either. Please
help me out.


Truly appreciated,
Jennifer


Private Sub dgNotes_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgNotes.UpdateCommand
Dim sPicked As String
Dim i As Integer
Dim FName As String
Dim LName As String
Dim N As String
Dim sUpdateQuery As String
Dim con As New ConnectDB()
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim RetVal As Int16

'******
'** THIS BIT BELOW IS NOT WORKING PROPERLY
'** NO VALUES ARE BEING RETURNED FROM THE CELLS

Dim sFeedback As String = e.Item.Cells(1).Text
Dim sProject As String = e.Item.Cells(2).Text
Dim sShift As Integer = e.Item.Cells(3).Text
Dim sDate As String = e.Item.Cells(4).Text
Dim sNote As String = e.Item.Cells(5).Text
'*******

sPicked = Request("N")
i = InStr(sPicked, ",")
FName = Trim(Mid(sPicked, i + 1))
LName = Trim(Left(sPicked, i - 1))
N = Me.User.Identity.Name.ToString


sUpdateQuery = "UPDATE Captured_Data " & _
"Set Feedback = '" & sFeedback & "', Project = '" & _
sProject & "', Shift = " & _
sShift & ", [Date] = '" & Date.Now & "',Information = '" & _
sNote & "', " & _
"Agent_FirstName = '" & FName & "', Agent_LastName = '" & _
LName & "', Note_Author = '" & _
N & "' Where Agent_LastName = '" & LName & _
"' and Agent_FirstName = '" & FName & "' and " & _
"[Date] = '" & sDate & "'"

txtNotes.Text = sUpdateQuery

cn = con.ConnectDB
cn.Open()
cmd = New SqlClient.SqlCommand(sUpdateQuery, cn)
cmd.CommandType = CommandType.Text
RetVal = cmd.ExecuteNonQuery()
cn.Close()
FillGrid()
txtFeedback.Text = ""
txtProject.Text = ""
txtShift.Text = ""
txtNotes.Text = ""
lblErr.Visible = False


End Sub
 
J

Jennifer

If you don't mind, I do have one more question. How do I prevent a
cell from being editable?

Thanks again!
Jennifer

hi jennifer

e.Item.Cells(n) actually refers to the table cell, and not the control
inside the table cell

use

dim tb as TextBox = CType(e.Item.Cells(n).Controls(0), TextBox)
Response.Write(tb.Text)

also, don't use dynamic sql. use parameters instead

cheers

neil
I've got a datagrid. I want to allow the user to edit in the grid
directly. I'm using ASP with VB as the code behind in Visual Studio
2002.

I've never had a need or desire to do this before, so I found a
Quickstart example at DotNetJunkies.com. I've modified it for my needs

and can't get it to work. Specifically, the part where I get the
edited cell values does not work. e.Item.Cells(1).Text returns
nothing. None of the other indexes returns anything either. Please
help me out.


Truly appreciated,
Jennifer


Private Sub dgNotes_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgNotes.UpdateCommand
Dim sPicked As String
Dim i As Integer
Dim FName As String
Dim LName As String
Dim N As String
Dim sUpdateQuery As String
Dim con As New ConnectDB()
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim RetVal As Int16

'******
'** THIS BIT BELOW IS NOT WORKING PROPERLY
'** NO VALUES ARE BEING RETURNED FROM THE CELLS

Dim sFeedback As String = e.Item.Cells(1).Text
Dim sProject As String = e.Item.Cells(2).Text
Dim sShift As Integer = e.Item.Cells(3).Text
Dim sDate As String = e.Item.Cells(4).Text
Dim sNote As String = e.Item.Cells(5).Text
'*******

sPicked = Request("N")
i = InStr(sPicked, ",")
FName = Trim(Mid(sPicked, i + 1))
LName = Trim(Left(sPicked, i - 1))
N = Me.User.Identity.Name.ToString


sUpdateQuery = "UPDATE Captured_Data " & _
"Set Feedback = '" & sFeedback & "', Project = '" & _
sProject & "', Shift = " & _
sShift & ", [Date] = '" & Date.Now & "',Information = '" & _
sNote & "', " & _
"Agent_FirstName = '" & FName & "', Agent_LastName = '" & _
LName & "', Note_Author = '" & _
N & "' Where Agent_LastName = '" & LName & _
"' and Agent_FirstName = '" & FName & "' and " & _
"[Date] = '" & sDate & "'"

txtNotes.Text = sUpdateQuery

cn = con.ConnectDB
cn.Open()
cmd = New SqlClient.SqlCommand(sUpdateQuery, cn)
cmd.CommandType = CommandType.Text
RetVal = cmd.ExecuteNonQuery()
cn.Close()
FillGrid()
txtFeedback.Text = ""
txtProject.Text = ""
txtShift.Text = ""
txtNotes.Text = ""
lblErr.Visible = False


End Sub
 
N

neilmcguigan

you should be able to set the readonly property for a column in the
html, like this:

<asp:DataGrid runat="Server" ID="grid1">
<Columns>
<asp:BoundColumn ReadOnly="True"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
If you don't mind, I do have one more question. How do I prevent a
cell from being editable?

Thanks again!
Jennifer

hi jennifer

e.Item.Cells(n) actually refers to the table cell, and not the control
inside the table cell

use

dim tb as TextBox = CType(e.Item.Cells(n).Controls(0), TextBox)
Response.Write(tb.Text)

also, don't use dynamic sql. use parameters instead

cheers

neil
I've got a datagrid. I want to allow the user to edit in the grid
directly. I'm using ASP with VB as the code behind in Visual Studio
2002.

I've never had a need or desire to do this before, so I found a
Quickstart example at DotNetJunkies.com. I've modified it for my needs

and can't get it to work. Specifically, the part where I get the
edited cell values does not work. e.Item.Cells(1).Text returns
nothing. None of the other indexes returns anything either. Please
help me out.


Truly appreciated,
Jennifer


Private Sub dgNotes_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgNotes.UpdateCommand
Dim sPicked As String
Dim i As Integer
Dim FName As String
Dim LName As String
Dim N As String
Dim sUpdateQuery As String
Dim con As New ConnectDB()
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim RetVal As Int16

'******
'** THIS BIT BELOW IS NOT WORKING PROPERLY
'** NO VALUES ARE BEING RETURNED FROM THE CELLS

Dim sFeedback As String = e.Item.Cells(1).Text
Dim sProject As String = e.Item.Cells(2).Text
Dim sShift As Integer = e.Item.Cells(3).Text
Dim sDate As String = e.Item.Cells(4).Text
Dim sNote As String = e.Item.Cells(5).Text
'*******

sPicked = Request("N")
i = InStr(sPicked, ",")
FName = Trim(Mid(sPicked, i + 1))
LName = Trim(Left(sPicked, i - 1))
N = Me.User.Identity.Name.ToString


sUpdateQuery = "UPDATE Captured_Data " & _
"Set Feedback = '" & sFeedback & "', Project = '" & _
sProject & "', Shift = " & _
sShift & ", [Date] = '" & Date.Now & "',Information = '" & _
sNote & "', " & _
"Agent_FirstName = '" & FName & "', Agent_LastName = '" & _
LName & "', Note_Author = '" & _
N & "' Where Agent_LastName = '" & LName & _
"' and Agent_FirstName = '" & FName & "' and " & _
"[Date] = '" & sDate & "'"

txtNotes.Text = sUpdateQuery

cn = con.ConnectDB
cn.Open()
cmd = New SqlClient.SqlCommand(sUpdateQuery, cn)
cmd.CommandType = CommandType.Text
RetVal = cmd.ExecuteNonQuery()
cn.Close()
FillGrid()
txtFeedback.Text = ""
txtProject.Text = ""
txtShift.Text = ""
txtNotes.Text = ""
lblErr.Visible = False


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top