Visual Studio 2005 used for everything below:
2 tables created--Physician and Practice
Physician has a field named practiceID (int) which should act as foreign key to Practive.id (both type int, BUT NO relationships created)
Dragged Physician table onto new .aspx page
SQL statement: SELECT Physician.id, Physician.lname, Physician.fname, Physician.mname, Physician.title, Physician.phone, Physician.address1, Physician.address2, Physician.city, Physician.state, Physician.zip, Practice.name FROM Physician INNER JOIN Practice ON Physician.practiceID = Practice.id ORDER BY Physician.lname, Physician.fname
Lastly, enabled paging, sorting, BUT when i enable editing--the gridview shrinks to zero columns.
I would like the last column, Practice.name, to be read-only text, but a dropdownlist when editing a GridView record (dropdownlist populated by option: Practice.name and value: Practice.id)
How do I accomplish this? I can't seem to make the standard foreign key relationship work.
Thanks in advance!
![]() |
0 |
![]() |
In order for your edit to work you would have to create a SQL UPDATE statement for the update with any needed parameters.
In order to get your dropdown in a gridview you would create a template field like this:
<asp:TemplateField HeaderText="Group" SortExpression="GroupID"> <ItemStyle HorizontalAlign="center" /> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("FaqGroup.Name") %>' /> </ItemTemplate> <EditItemTemplate> <asp:Dropdownlist ID="ddl2" runat="server" DataSourceId="lnqFaqGroups" DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("GroupID") %>' /> </EditItemTemplate> </asp:TemplateField>
Remember to mark as answer if this post answered your question.
![]() |
0 |
![]() |
I followed most of that. I have created an UPDATE statement:
UPDATE Physician SET lname = @lname, fname = @fname, mname = @mname, title = @title, practiceID = @practiceID, phone = @phone, address1 = @address1, address2 = @address2, city = @city, state = @state, zip = @zip WHERE (id = @id)
And added the ItemTemplate and EditItemTemplate controls above
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Practice.name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Dropdownlist ID="ddl2" runat="server" DataSourceId="SqlDataSource1" DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("Practice.id") %>' />
</EditItemTemplate>
</asp:TemplateField>however, I do not understand Bind("FaqGroup.Name") and Bind("GroupID")
I tried for the first instance, Bind ("Practice.name") and for the second instance Bind ("Practice.id") but those don't seem to work.
Also, my GridView is still collapsed (as if it were "broken" ? ) when I check the "enable editing" checkbox
Receive bad formatted Bind error...not sure what Bind is really for
I have successfully tested the UPDATE and INSERT queries. Thanks so far!
![]() |
0 |
![]() |
I am using LINQ to SQL as my datasource and FaqGroup is a table which has a FK of ID and a field of Name in it so I can connect to the FaqGroup table by using FaqGroup.Name and display the name in the ddl.
In your case you can do it in SQL and just set DataTextField = Eval("Name") and DataValueField = Bind("ID").
One thing you may want to do to make things easier is put your datasource in with the template field you are using it in. For instance:
<asp:TemplateField HeaderText="Name"><ItemTemplate><asp:Label ID="Label3" runat="server" Text='<%# Bind("Practice.name") %>' /></ItemTemplate>
<EditItemTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectStatment"...... /><asp:Dropdownlist ID="ddl2" runat="server" DataSourceId="SqlDataSource1" DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("Practice.id") %>' /></EditItemTemplate>
</asp:TemplateField>
Remember to mark as answer if this post answered your question.
![]() |
0 |
![]() |
see following posts...
![]() |
0 |
![]() |
http://www.asp.net/learn/data-access/tutorial-20-vb.aspx
This actually looks exactly like what I'd like to do. I don't follow Step 1: Creating the Appropriate
UpdateProduct
OverloadWhat does
<System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, False)> mean?
Is this tag generated automatically when you add the method?
![]() |
0 |
![]() |
aha! I have progress!
I have just added 2 fields (Physician.lname and Practice.name) at this point, but now my updates do not work. I click edit, change the DDL or TextBox, click update, no change is saved...
code:
1 <asp:GridView ID="GridView1" runat="server" 2 AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display." AllowPaging="True" AllowSorting="True"> 3 <Columns> 4 <asp:CommandField ShowEditButton="True" /> 5 <asp:TemplateField HeaderText="Practice" SortExpression="name"> 6 <EditItemTemplate> 7 <asp:DropdownList ID="ddl2" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="PracticeID" SelectedValue='<%# Bind("PracticeID") %>'></asp:DropdownList> 8 </EditItemTemplate> 9 <ItemTemplate> 10 <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label> 11 </ItemTemplate> 12 </asp:TemplateField> 13 <asp:TemplateField HeaderText="Last Name" SortExpression="lname"> 14 <EditItemTemplate> 15 <asp:TextBox ID="lname" runat="server" Text='<%# Bind("lname") %>'></asp:TextBox> 16 </EditItemTemplate> 17 <ItemTemplate> 18 <asp:Label ID="Label2" runat="server" Text='<%# Bind("lname") %>'></asp:Label> 19 </ItemTemplate> 20 </asp:TemplateField> 21 </Columns> 22 </asp:GridView> 23 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mdsc-marketConnectionString1 %>" 24 DeleteCommand="DELETE FROM [Physician] WHERE [PhysicianID] = @PhysicianID" 25 ProviderName="<%$ ConnectionStrings:mdsc-marketConnectionString1.ProviderName %>" 26 SelectCommand="SELECT Physician.lname, Physician.fname, Physician.mname, Physician.title, Physician.phone, Physician.address1, Physician.address2, Physician.city, Physician.state, Physician.zip, Practice.name, Practice.PracticeID FROM Physician INNER JOIN Practice ON Physician.practiceID = Practice.PracticeID" 27 UpdateCommand="UPDATE [Physician] SET [lname] = @lname, [fname] = @fname, [mname] = @mname, [title] = @title, [practiceID] = @practiceID, [phone] = @phone, [address1] = @address1, [address2] = @address2, [city] = @city, [state] = @state, [zip] = @zip WHERE [PhysicianID] = @PhysicianID"> 28 <DeleteParameters> 29 <asp:Parameter Name="PhysicianID" Type="Int32" /> 30 </DeleteParameters> 31 <UpdateParameters> 32 <asp:Parameter Name="lname" Type="String" /> 33 <asp:Parameter Name="fname" Type="String" /> 34 <asp:Parameter Name="mname" Type="String" /> 35 <asp:Parameter Name="title" Type="String" /> 36 <asp:Parameter Name="practiceID" Type="Int32" /> 37 <asp:Parameter Name="phone" Type="String" /> 38 <asp:Parameter Name="address1" Type="String" /> 39 <asp:Parameter Name="address2" Type="String" /> 40 <asp:Parameter Name="city" Type="String" /> 41 <asp:Parameter Name="state" Type="String" /> 42 <asp:Parameter Name="zip" Type="String" /> 43 <asp:Parameter Name="PhysicianID" Type="Int32" /> 44 </UpdateParameters> 45 <InsertParameters> 46 <asp:Parameter Name="lname" Type="String" /> 47 <asp:Parameter Name="fname" Type="String" /> 48 <asp:Parameter Name="mname" Type="String" /> 49 <asp:Parameter Name="title" Type="String" /> 50 <asp:Parameter Name="practiceID" Type="Int32" /> 51 <asp:Parameter Name="phone" Type="String" /> 52 <asp:Parameter Name="address1" Type="String" /> 53 <asp:Parameter Name="address2" Type="String" /> 54 <asp:Parameter Name="city" Type="String" /> 55 <asp:Parameter Name="state" Type="String" /> 56 <asp:Parameter Name="zip" Type="String" /> 57 </InsertParameters> 58 </asp:SqlDataSource>Thanks so far! Your sample code above has been a huge help!
![]() |
0 |
![]() |
As I mentioned before you should create a seperate datasource for your nested dropdownlist. Here is an example and it works just fine for me. Granted, I don't have nearly as complex update process as you do but it works. Here is a brief explination of how the DDL works inside the edit template. Create a datasource that gathers the info from the primary key table. In my case its the FaqGroups table which has ID and Name. Set the display (Name) and value (ID) of the ddl to the two fields in the SQL datasource. Set the selected index property of the ddl to the Foregin Key in the Faqs table which is GroupID. This way when the row is updated it will update the GroupId with the selectedvalue of the ddl.
So in your case you are looking for the practice name and practice id. Create a sql datasource with this select statement "SELECT DISTINCT Name, PracticeId FROM Practice ORDER BY Name" This will get you the list to populate the ddl with. The set the display and value fields and the selectvalue should be set to your FK field in your Physician table.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" DeleteCommand="DELETE FROM [Faq] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Faq] ([Question], [Answer], [GroupID], [CreateDate]) VALUES (@Question, @Answer, @GroupID, @CreateDate)" SelectCommand="SELECT [ID], [Question], [Answer], [GroupID], [CreateDate] FROM [Faq] ORDER BY [Question]" UpdateCommand="UPDATE [Faq] SET [Question] = @Question, [Answer] = @Answer, [GroupID] = @GroupID, [CreateDate] = @CreateDate WHERE [ID] = @ID"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Question" Type="String" /> <asp:Parameter Name="Answer" Type="String" /> <asp:Parameter Name="GroupID" Type="Int32" /> <asp:Parameter Name="CreateDate" Type="DateTime" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="Question" Type="String" /> <asp:Parameter Name="Answer" Type="String" /> <asp:Parameter Name="GroupID" Type="Int32" /> <asp:Parameter Name="CreateDate" Type="DateTime" /> </InsertParameters> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Question" HeaderText="Question" SortExpression="Question" /> <asp:BoundField DataField="Answer" HeaderText="Answer" SortExpression="Answer" /> <asp:TemplateField HeaderText="Group" SortExpression="GroupID"> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("GroupID") %>'> </asp:DropDownList> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" SelectCommand="SELECT DISTINCT [Name], [ID] FROM [FaqGroup] ORDER BY [Name]"> </asp:SqlDataSource> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("GroupID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="CreateDate" HeaderText="CreateDate" SortExpression="CreateDate" DataFormatString="{0:d}" /> </Columns> </asp:GridView>
Remember to mark as answer if this post answered your question.
![]() |
0 |
![]() |
you ROCK. I did add the secondary data source, as I didn't see where you mentioned it above--could've just been a 2am reading ;)
I also had to add a (declaration?) to the gridview control datakeynames -- whats this all about?
thank you so much!
![]() |
0 |
![]() |
Here is the technical info about it.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeynames.aspx
Essentially it is a primary key or group of primary keys used to uniquely identify a row in the gridview and are passed to the datasource when updating and deleting.
Remember to mark as answer if this post answered your question.
![]() |
0 |
![]() |