 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 15th, 2004, 11:59 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Retrieving Primary key in DataGrid
I have DataKeyField Property set in my DataGrid and I can retrieve its value using
Dim CustomerID As String = dg_Customers.DataKeys(e.Item.ItemIndex)
but, when I use another approach (hidden bound column) I am having difficulty reteriveing its value. What I am doing wrong? Please advise. Earlier approach won't work If i have composite primary key
'Retrieving primarykey
Dim CustomerID As String = e.Item.Cells(2).Text
'My datagrid declaration
<asp:DataGrid id="dg_Customers" runat="server" AutoGenerateColumns="False" OnEditCommand="dg_Edit"
OnCancelCommand="dg_Cancel" OnUpdateCommand="dg_Update" OnDeleteCommand="dg_Delete" DataKeyField="CustomerID">
<Columns>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit Info" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" HeaderText="Company Name"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactName" HeaderText="Contact Name"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactTitle" HeaderText="Contact Title"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="CustomerId" DataNavigateUrlFormatString="edit_customer.aspx?Cu stomerId={0}"
DataTextField="CustomerID" HeaderText="View Details"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>
PS. I am working with northwind.mdb, Customers table and customerID is MS Access text data type
|
|

June 16th, 2004, 01:04 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Can you elaborate on what exactly the problem/difficulty is?
Are you getting an error?
|
|

June 16th, 2004, 04:18 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Not getting an error.
my update module is not working, it works fine when I use
Dim CustomerID As String = dg_Customers.DataKeys(e.Item.ItemIndex)
but it is not working when I use (hidden bound control instead of datakeyfield value
'datagrid code is posted earlier
Dim CustomerID As String = e.Item.Cells(2).Text
Below is the code for Update Sub. Thanks in advance
Sub dg_Update(ByVal Sender As Object, ByVal e As
DataGridCommandEventArgs)
'Dim CustomerID As String = dg_Customers.DataKeys(e.Item.ItemIndex)
Dim CustomerID As String = e.Item.Cells(2).Text
Dim CompanyName As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim ContactName As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim ContactTitle As String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
Dim update_SQL As String = "UPDATE [Customers] SET CompanyName = ?, " & _
"ContactName = ?, ContactTitle = ? WHERE [CustomerID]=" & _
"?"
Dim objConn As New OleDbConnection(ConfigurationSettings.AppSettings( "ConnectionString"))
Dim myCmd As New OleDbCommand(update_SQL, objConn)
myCmd.Parameters.Add(New OleDbParameter("CompanyName", CompanyName))
myCmd.Parameters.Add(New OleDbParameter("ContactName", ContactName))
myCmd.Parameters.Add(New OleDbParameter("ContactTitle", ContactTitle))
myCmd.Parameters.Add(New OleDbParameter("CustomerID", CustomerID))
objConn.Open()
myCmd.ExecuteNonQuery()
objConn.Close()
dg_Customers.EditItemIndex = -1
BindData()
End Sub
|
|

June 16th, 2004, 10:18 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
I suspect the problem you are having has to do with the behavior of the column.
<asp:BoundColumn Visible="False" DataField="CustomerID"></asp:BoundColumn>
This is a standard bound column, meaning that when a grid item is in edit mode, the contents of the column will be contained within a textbox control, not just as plain text. So when you ask for e.Item.Cells(2).Text you get nothing because there is no text content, but rather a single textbox control (just like in the standard editable visible bound columns). Make the column readonly and it should work.
<asp:BoundColumn Visible="False" DataField="CustomerID" ReadOnly="true"></asp:BoundColumn>
Peter
-------------------------
Work smarter, not harder
|
|

June 17th, 2004, 07:47 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I changed the Readonly Property to True and it is working now. Thank you Peter, I appreciate your help
|
|

June 17th, 2004, 01:15 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Glad I could help.
It took me some time to figure that idea out when I was first learning datagrids. I'm curious why you aren't just using the datakeys collection. The only reason I can think that you would need to use a hidden column is it you have more than just a single key field (which is very likely).
|
|

June 17th, 2004, 01:22 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You got it right. Datakeys collection won't work when you have composite primary key? I did mention it in my first post.
|
|

June 17th, 2004, 01:26 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Oh right. My bad. I read a lot of posts, so I occassionally miss details. 
|
|

August 7th, 2004, 02:14 PM
|
|
Authorized User
|
|
Join Date: Aug 2004
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sanchi, I saw this post and wanted to ask you a question. I am doing something similar to you. I need the value of the column(primary key) from my datagrid and based on the key, I want to move to a detail page with detail information for editing. I tried to you datakeyfield property and used the e.item.itemindex, but I get a compilation error that " mytablename is not declared". what am i missing. Please help!
Renu
Quote:
quote:Originally posted by shahchi1
I have DataKeyField Property set in my DataGrid and I can retrieve its value using
Dim CustomerID As String = dg_Customers.DataKeys(e.Item.ItemIndex)
but, when I use another approach (hidden bound column) I am having difficulty reteriveing its value. What I am doing wrong? Please advise. Earlier approach won't work If i have composite primary key
'Retrieving primarykey
Dim CustomerID As String = e.Item.Cells(2).Text
'My datagrid declaration
<asp:DataGrid id="dg_Customers" runat="server" AutoGenerateColumns="False" OnEditCommand="dg_Edit"
OnCancelCommand="dg_Cancel" OnUpdateCommand="dg_Update" OnDeleteCommand="dg_Delete" DataKeyField="CustomerID">
<Columns>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit Info" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" HeaderText="Company Name"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactName" HeaderText="Contact Name"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactTitle" HeaderText="Contact Title"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="CustomerId" DataNavigateUrlFormatString="edit_customer.aspx?Cu stomerId={0}"
DataTextField="CustomerID" HeaderText="View Details"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>
PS. I am working with northwind.mdb, Customers table and customerID is MS Access text data type
|
|
|

August 8th, 2004, 09:35 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
Can you tell us what "mytablename" is?
|
|
 |