P
P. Yanzick
Hello,
I've been playing with master/detail views as well as editing in the
gridview, and I ran across a strange problem that I am not exactly sure
where to go to try to solve.
I have 2 tables, a table of cars (pretty basic, an ID, a description, and a
Color ID) and a table of colors (Color ID, and a color description). I've
added a gridview and a detailsview as I'm playing with both and how to get
editing features to work the way I want to, and in this case I want a
dropdown box to appear with the color description, then write the ID to the
database.
Right now I am just getting the editing functionality working, however I
noticed something odd. In both the gridview if I use edit, or if I edit in
the detailsview, the color value is always off by 1, 1 higher than it should
be. Hoping to get an out of range error, I edited a record with the highest
color value, but instead of an error, it then moved to the lowest value
possible in the dropdown.
The code is below, if you are interested. I'd like it to default to what
the value actually is. Would anyone have any suggestions for me???
Thanks
Paul
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="CarDisplayDS" runat="server" ConnectionString="<%$
ConnectionStrings:TestDBConnectionString %>"
SelectCommand="SELECT Car.CarID, Car.CarDescription, Color.ColorDescription,
Car.ColorID FROM Car INNER JOIN Color ON Car.ColorID = Color.ColorID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="ColorListForDropDownDS" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="SELECT ColorID, ColorDescription FROM
Color"></asp:SqlDataSource>
<asp:SqlDataSource ID="DetailCarDisplayDS" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
InsertCommand="INSERT INTO Car(CarDescription, ColorID) VALUES (,)"
SelectCommand="SELECT Car.CarID, Car.CarDescription, Car.ColorID,
Color.ColorDescription FROM Car INNER JOIN Color ON Car.ColorID =
Color.ColorID"
UpdateCommand="UPDATE Car SET CarDescription =, ColorID = FROM Car INNER
JOIN Color ON Car.ColorID = Color.ColorID"
FilterExpression="CarID={0}">
<FilterParameters>
<asp:ControlParameter
Name="CarID"
ControlID="GridView1"
PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="CarID"
DataSourceID="CarDisplayDS" GridLines="Vertical"
AutoGenerateSelectButton="True" >
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="CarID" HeaderText="CarID" InsertVisible="False"
ReadOnly="True"
SortExpression="CarID" />
<asp:BoundField DataField="CarDescription" HeaderText="CarDescription"
SortExpression="Car Description" />
<asp:TemplateField HeaderText="ColorDescription" SortExpression="Color
Description">
<EditItemTemplate>
<aspropDownList ID="DropDownList2" runat="server"
DataSourceID="ColorListForDropDownDS"
DataTextField="ColorDescription" DataValueField="ColorID" SelectedIndex='<%#
Bind("ColorID") %>'>
</aspropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ColorDescription")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ColorID" HeaderText="ColorID"
SortExpression="ColorID"
Visible="False" />
</Columns>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center"
/>
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="CarID"
DataSourceID="DetailCarDisplayDS"
DefaultMode="Edit">
<EditItemTemplate>
CarID:
<asp:Label ID="CarIDLabel1" runat="server" Text='<%# Eval("CarID")
%>'></asp:Label><br />
CarDescription:
<asp:TextBox ID="CarDescriptionTextBox" runat="server" Text='<%#
Bind("CarDescription") %>'></asp:TextBox><br />
ColorDescription:
<aspropDownList ID="DropDownList1" runat="server"
DataSourceID="ColorListForDropDownDS"
DataTextField="ColorDescription" DataValueField="ColorID" SelectedIndex='<%#
Eval("ColorID") %>'>
</aspropDownList><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
CarDescription:
<asp:TextBox ID="CarDescriptionTextBox" runat="server" Text='<%#
Bind("CarDescription") %>'>
</asp:TextBox><br />
ColorDescription:
<asp:TextBox ID="ColorDescriptionTextBox" runat="server" Text='<%#
Bind("ColorDescription") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
CarID:
<asp:Label ID="CarIDLabel" runat="server" Text='<%# Eval("CarID")
%>'></asp:Label><br />
CarDescription:
<asp:Label ID="CarDescriptionLabel" runat="server" Text='<%#
Bind("CarDescription") %>'></asp:Label><br />
ColorDescription:
<asp:Label ID="ColorDescriptionLabel" runat="server" Text='<%#
Bind("ColorDescription") %>'></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit"
Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New"
Text="New"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>
I've been playing with master/detail views as well as editing in the
gridview, and I ran across a strange problem that I am not exactly sure
where to go to try to solve.
I have 2 tables, a table of cars (pretty basic, an ID, a description, and a
Color ID) and a table of colors (Color ID, and a color description). I've
added a gridview and a detailsview as I'm playing with both and how to get
editing features to work the way I want to, and in this case I want a
dropdown box to appear with the color description, then write the ID to the
database.
Right now I am just getting the editing functionality working, however I
noticed something odd. In both the gridview if I use edit, or if I edit in
the detailsview, the color value is always off by 1, 1 higher than it should
be. Hoping to get an out of range error, I edited a record with the highest
color value, but instead of an error, it then moved to the lowest value
possible in the dropdown.
The code is below, if you are interested. I'd like it to default to what
the value actually is. Would anyone have any suggestions for me???
Thanks
Paul
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="CarDisplayDS" runat="server" ConnectionString="<%$
ConnectionStrings:TestDBConnectionString %>"
SelectCommand="SELECT Car.CarID, Car.CarDescription, Color.ColorDescription,
Car.ColorID FROM Car INNER JOIN Color ON Car.ColorID = Color.ColorID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="ColorListForDropDownDS" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
SelectCommand="SELECT ColorID, ColorDescription FROM
Color"></asp:SqlDataSource>
<asp:SqlDataSource ID="DetailCarDisplayDS" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"
InsertCommand="INSERT INTO Car(CarDescription, ColorID) VALUES (,)"
SelectCommand="SELECT Car.CarID, Car.CarDescription, Car.ColorID,
Color.ColorDescription FROM Car INNER JOIN Color ON Car.ColorID =
Color.ColorID"
UpdateCommand="UPDATE Car SET CarDescription =, ColorID = FROM Car INNER
JOIN Color ON Car.ColorID = Color.ColorID"
FilterExpression="CarID={0}">
<FilterParameters>
<asp:ControlParameter
Name="CarID"
ControlID="GridView1"
PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="CarID"
DataSourceID="CarDisplayDS" GridLines="Vertical"
AutoGenerateSelectButton="True" >
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="CarID" HeaderText="CarID" InsertVisible="False"
ReadOnly="True"
SortExpression="CarID" />
<asp:BoundField DataField="CarDescription" HeaderText="CarDescription"
SortExpression="Car Description" />
<asp:TemplateField HeaderText="ColorDescription" SortExpression="Color
Description">
<EditItemTemplate>
<aspropDownList ID="DropDownList2" runat="server"
DataSourceID="ColorListForDropDownDS"
DataTextField="ColorDescription" DataValueField="ColorID" SelectedIndex='<%#
Bind("ColorID") %>'>
</aspropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ColorDescription")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ColorID" HeaderText="ColorID"
SortExpression="ColorID"
Visible="False" />
</Columns>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center"
/>
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="CarID"
DataSourceID="DetailCarDisplayDS"
DefaultMode="Edit">
<EditItemTemplate>
CarID:
<asp:Label ID="CarIDLabel1" runat="server" Text='<%# Eval("CarID")
%>'></asp:Label><br />
CarDescription:
<asp:TextBox ID="CarDescriptionTextBox" runat="server" Text='<%#
Bind("CarDescription") %>'></asp:TextBox><br />
ColorDescription:
<aspropDownList ID="DropDownList1" runat="server"
DataSourceID="ColorListForDropDownDS"
DataTextField="ColorDescription" DataValueField="ColorID" SelectedIndex='<%#
Eval("ColorID") %>'>
</aspropDownList><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
CarDescription:
<asp:TextBox ID="CarDescriptionTextBox" runat="server" Text='<%#
Bind("CarDescription") %>'>
</asp:TextBox><br />
ColorDescription:
<asp:TextBox ID="ColorDescriptionTextBox" runat="server" Text='<%#
Bind("ColorDescription") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
CarID:
<asp:Label ID="CarIDLabel" runat="server" Text='<%# Eval("CarID")
%>'></asp:Label><br />
CarDescription:
<asp:Label ID="CarDescriptionLabel" runat="server" Text='<%#
Bind("CarDescription") %>'></asp:Label><br />
ColorDescription:
<asp:Label ID="ColorDescriptionLabel" runat="server" Text='<%#
Bind("ColorDescription") %>'></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit"
Text="Edit"></asp:LinkButton>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New"
Text="New"></asp:LinkButton>
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>