Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid update/edit problem 1

Status
Not open for further replies.

doubletalkin3

Programmer
Joined
Apr 24, 2006
Messages
6
Location
GB
Hi,

I'm trying to implement an update command on a datagrid, but on clicking update, nothing happens, and I can't see why. I've used strings instead of parameters in the sqlcommand and this works, and also passed straight strings into the parameters - this also works.

It looks like something is wrong with the converting to textbox lines in the update function.

I've tried lots of different things with my code and this is how it currently looks. Thanks in advance for any help -

---------------------------------------------

Sub BindGrid()

dAdap.Fill(dSet)
grdTelephoneList.DataSource = dSet
grdTelephoneList.DataKeyField = "RecordID"
grdTelephoneList.DataBind()

End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
BindGrid()
End If

SqlConnection1.Open()

strSQLQuery = "SELECT RecordID, Forename, Surname, JobTitle, Extension, Mobile, Department FROM Contacts ORDER BY Surname"
dAdap = New SqlDataAdapter(strSQLQuery, SqlConnection1)

dSet = New DataSet
dAdap.Fill(dSet)

grdTelephoneList.DataSource = dSet
grdTelephoneList.DataKeyField = "RecordID"
grdTelephoneList.DataBind()

SqlConnection1.Close()

End Sub


Private Sub grdTelephoneList_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.EditCommand
grdTelephoneList.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub

Private Sub grdTelephoneList_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.CancelCommand
grdTelephoneList.EditItemIndex = -1
BindGrid()
End Sub

Private Sub grdTelephoneList_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grdTelephoneList.UpdateCommand
Dim strForename As String = CType(e.Item.Cells(0).Controls(0), TextBox).Text
Dim strSurname As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text

SqlCommand1.Parameters("@Forename").Value = strForename
SqlCommand1.Parameters("@Surname").Value = strSurname
SqlCommand1.Parameters("@RecordID").Value = grdTelephoneList.DataKeys(e.Item.ItemIndex)

SqlConnection1.Open()
SqlCommand1.ExecuteNonQuery()

grdTelephoneList.EditItemIndex = -1
BindGrid()
SqlConnection1.Close()

End Sub
 
Here's the HTML -

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="IntranetAdmin.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
<LINK href="Styles.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="grdTelephoneList" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 48px"
runat="server" AutoGenerateColumns="False" Height="224px" Width="90%">
<Columns>
<asp:BoundColumn DataField="Forename" HeaderText="Forename"></asp:BoundColumn>
<asp:BoundColumn DataField="Surname" HeaderText="Surname"></asp:BoundColumn>
<asp:BoundColumn DataField="JobTitle" HeaderText="Job Title"></asp:BoundColumn>
<asp:BoundColumn DataField="Extension" HeaderText="Extension"></asp:BoundColumn>
<asp:BoundColumn DataField="Mobile" HeaderText="Mobile Number"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:datagrid><asp:label id="LblError" runat="server">Label</asp:label>
<asp:Label id="Label1" runat="server">Label</asp:Label>
<asp:Label id="Label2" runat="server">Label</asp:Label></form>
</body>
</HTML>
 
doubletalking: I'm on the road right now and so not able to take a close look - I would recommend that you do a search here on Tek-Tips (use the Search Tab) and do a general overview of "datagrid updage" or "datagrid edit". There are several dozen threads here that take a close look at this process. Do this until someone drops by and perhaps sees the exact problem. One issue that strikes me is setting the SQL parameters and not using a WHERE statement.
 
Your missing a few things here.
In the html for your grid you need to specify the update command...e.g. OnUpdateCommand="YourSubName"

also, wheres your SQL for your update?

You also should specify your dbtype and parameters a little better.


I hate to keep giving this link but this is very very straight forward and will show you the basic's of the grid.
 
Thanks for the help. The problem was to do with the post back on the page load. The page was a mess of visual studio wizards and manual declarations (hence no sql statement) - I've now sorted all this out too
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top