javagair
asked on
How do I bind a field from a sql server to a dropdownlist
Working on a web site
have a gridview and one of the columns is a dropdownlist
2 items: none and pto
When you open the web page it should display what you selected the last time you opened it.
all of the bound text boxes work, but the dropdownlist shows the first item of the list and not the item choosen and stored in the sql database.
the web page reads the value for every other column in dataview, why not the dropdownlist?
Do I have to do some time of binding between the sql and the dropdownlist?
thanks gair
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="fALSE" AutoGenerateColumns="False "
DataSourceID="SqlDataSourc e1">
<Columns>
<asp:BoundField DataField="WORKDATE" HeaderText="DATE" ReadOnly="True" />
<asp:BoundField DataField="STARTDAY" HeaderText="DAY" />
<asp:BoundField DataField="STARTTIME" HeaderText="IN" />
<asp:BoundField DataField="ENDTIME" HeaderText="OUT" />
<asp:BoundField DataField="STARTLUNCH" HeaderText="LUNCH" />
<asp:BoundField DataField="ENDLUNCH" HeaderText="LUNCH BACK" />
<asp:BoundField DataField="PTODETAIL" HeaderText="DETAIL PTO" ReadOnly="True" />
<asp:TemplateField HeaderText="Pto Detail">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataTextField="PTO" runat="server">
<asp:ListItem>NONE</asp:Li stItem>
<asp:ListItem>PTO</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PTOPAID" HeaderText="PTO HR" />
<asp:TemplateField HeaderText="Company PTO">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" DataTextField="COMPANYPTO" runat="server" DataValueField="COMPANYPTO ">
<asp:ListItem>NONE</asp:Li stItem>
<asp:ListItem>Paid Holiday</asp:ListItem>
<asp:ListItem>Bereviament< /asp:ListI tem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="COMPANYPTOPAID" HeaderText="COMPANY HRS" />
<asp:BoundField DataField="SYMITARCODE" HeaderText="ESPY CODE" />
<asp:TemplateField HeaderText="EDIT">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" Text="edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="update" />
<asp:Button ID="Button2" runat="server" Text="cancel" />
<asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:wages1SQ LConnectio nString %>"
SelectCommand="SELECT [WORKDATE], [STARTDAY], [STARTTIME], [ENDTIME], [STARTLUNCH], [ENDLUNCH], [PTODETAIL], [PTOPAID], [PTOUNPAID], [COMPANYPTO], [COMPANYPTOPAID], [SYMITARCODE] FROM [EmployeeAccumInfo]"></asp :sqldataso urce>
</div>
have a gridview and one of the columns is a dropdownlist
2 items: none and pto
When you open the web page it should display what you selected the last time you opened it.
all of the bound text boxes work, but the dropdownlist shows the first item of the list and not the item choosen and stored in the sql database.
the web page reads the value for every other column in dataview, why not the dropdownlist?
Do I have to do some time of binding between the sql and the dropdownlist?
thanks gair
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="fALSE" AutoGenerateColumns="False
DataSourceID="SqlDataSourc
<Columns>
<asp:BoundField DataField="WORKDATE" HeaderText="DATE" ReadOnly="True" />
<asp:BoundField DataField="STARTDAY" HeaderText="DAY" />
<asp:BoundField DataField="STARTTIME" HeaderText="IN" />
<asp:BoundField DataField="ENDTIME" HeaderText="OUT" />
<asp:BoundField DataField="STARTLUNCH" HeaderText="LUNCH" />
<asp:BoundField DataField="ENDLUNCH" HeaderText="LUNCH BACK" />
<asp:BoundField DataField="PTODETAIL" HeaderText="DETAIL PTO" ReadOnly="True" />
<asp:TemplateField HeaderText="Pto Detail">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataTextField="PTO" runat="server">
<asp:ListItem>NONE</asp:Li
<asp:ListItem>PTO</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PTOPAID" HeaderText="PTO HR" />
<asp:TemplateField HeaderText="Company PTO">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" DataTextField="COMPANYPTO"
<asp:ListItem>NONE</asp:Li
<asp:ListItem>Paid Holiday</asp:ListItem>
<asp:ListItem>Bereviament<
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="COMPANYPTOPAID"
<asp:BoundField DataField="SYMITARCODE" HeaderText="ESPY CODE" />
<asp:TemplateField HeaderText="EDIT">
<ItemTemplate>
<asp:Button ID="Button3" runat="server" Text="edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="update" />
<asp:Button ID="Button2" runat="server" Text="cancel" />
<asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:wages1SQ
SelectCommand="SELECT [WORKDATE], [STARTDAY], [STARTTIME], [ENDTIME], [STARTLUNCH], [ENDLUNCH], [PTODETAIL], [PTOPAID], [PTOUNPAID], [COMPANYPTO], [COMPANYPTOPAID], [SYMITARCODE] FROM [EmployeeAccumInfo]"></asp
</div>
ASKER
this one gives error
<asp:TemplateField HeaderText="Pto Detail">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue="<%# Bind("PTODETAIL") %>">
<asp:ListItem>NONE</asp:Li stItem>
<asp:ListItem>PTO</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
this one does not
<asp:TemplateField HeaderText="Company PTO" >
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue="<%# Bind("COMPANYPTO") %>">
<asp:ListItem>NONE</asp:Li stItem>
<asp:ListItem>Paid Holiday</asp:ListItem>
<asp:ListItem>Bereviament< /asp:ListI tem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
if I don't change the single quotes to double it errors immediately, am I doing something wrong, regardless with the double quotes one gives no error that the other does.
I do not see any difference.
gair
<asp:TemplateField HeaderText="Pto Detail">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue="<%# Bind("PTODETAIL") %>">
<asp:ListItem>NONE</asp:Li
<asp:ListItem>PTO</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
this one does not
<asp:TemplateField HeaderText="Company PTO" >
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue="<%# Bind("COMPANYPTO") %>">
<asp:ListItem>NONE</asp:Li
<asp:ListItem>Paid Holiday</asp:ListItem>
<asp:ListItem>Bereviament<
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
if I don't change the single quotes to double it errors immediately, am I doing something wrong, regardless with the double quotes one gives no error that the other does.
I do not see any difference.
gair
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ACTUALLY NEITHER WORK
this is the runtime error that occurs:
[ArgumentOutOfRangeExcepti on: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items.
THIS IS MY TEST CODE
Parameter name: value]
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button3" runat="server" Text="edit" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="False"
AllowSorting="fALSE" AutoGenerateColumns="False "
DataSourceID="SqlDataSourc e1">
<Columns>
<asp:BoundField DataField="WORKDATE" HeaderText="DATE" ReadOnly="True" ApplyFormatInEditMode="Fal se" />
<asp:BoundField DataField="STARTDAY" HeaderText="DAY" ReadOnly="True" />
<asp:TemplateField HeaderText="in">
<ItemTemplate>
<asp:Label runat="server" ID="STARTTIME" Text="<%#Eval(“startTime”) %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="StartTime" Text="<%#Eval(“starttime”) %>" />
<asp:RequiredFieldValidato r runat="server" ID="rfStartTime" ControlToValidate="StartTi me" ValidationGroup="var1" ErrorMessage=" " />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ENDTIME" HeaderText="OUT" ReadOnly="True" ApplyFormatInEditMode="Tru e" />
<asp:BoundField DataField="STARTLUNCH" HeaderText="LUNCH" ReadOnly="True" ApplyFormatInEditMode="Tru e" />
<asp:BoundField DataField="ENDLUNCH" HeaderText="LUNCH BACK" ApplyFormatInEditMode="Tru e" />
<asp:TemplateField HeaderText="Pto Detail">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("COMPANYPTO") %>'>
<asp:ListItem>NONE</asp:Li stItem>
<asp:ListItem>PTO</asp:Lis tItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PTOPAID" HeaderText="PTO HR" ReadOnly="True" ApplyFormatInEditMode="Tru e" />
<asp:BoundField DataField="PTOUNPAID" HeaderText="PTO UnPaid" ReadOnly="True" ApplyFormatInEditMode="Tru e" />
<asp:TemplateField HeaderText="Company PTO" >
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("COMPANYPTO") %>'>
<asp:ListItem>NONE</asp:Li stItem>
<asp:ListItem>Paid Holiday</asp:ListItem>
<asp:ListItem>Bereviament< /asp:ListI tem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="COMPANYPTOPAID" HeaderText="COMPANY HRS" ReadOnly="True" ApplyFormatInEditMode="Tru e" />
<asp:BoundField DataField="SYMITARCODE" HeaderText="ESPY CODE" ReadOnly="True" ApplyFormatInEditMode="Tru e" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="update" />
<asp:Button ID="Button2" runat="server" Text="cancel" />
<asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:wages1SQ LConnectio nString %>"
SelectCommand="SELECT [WORKDATE], [STARTDAY], [STARTTIME], [ENDTIME], [STARTLUNCH], [ENDLUNCH], [PTODETAIL], [PTOPAID], [PTOUNPAID], [COMPANYPTO], [COMPANYPTOPAID], [SYMITARCODE] FROM [EmployeeAccumInfo]"></asp :sqldataso urce>
</div>
</form>
</body>
</html>
value is the same as what I am bringing out of database
thanks
gary
this is the runtime error that occurs:
[ArgumentOutOfRangeExcepti
THIS IS MY TEST CODE
Parameter name: value]
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button3" runat="server" Text="edit" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="False"
AllowSorting="fALSE" AutoGenerateColumns="False
DataSourceID="SqlDataSourc
<Columns>
<asp:BoundField DataField="WORKDATE" HeaderText="DATE" ReadOnly="True" ApplyFormatInEditMode="Fal
<asp:BoundField DataField="STARTDAY" HeaderText="DAY" ReadOnly="True" />
<asp:TemplateField HeaderText="in">
<ItemTemplate>
<asp:Label runat="server" ID="STARTTIME" Text="<%#Eval(“startTime”)
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="StartTime" Text="<%#Eval(“starttime”)
<asp:RequiredFieldValidato
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ENDTIME" HeaderText="OUT" ReadOnly="True" ApplyFormatInEditMode="Tru
<asp:BoundField DataField="STARTLUNCH" HeaderText="LUNCH" ReadOnly="True" ApplyFormatInEditMode="Tru
<asp:BoundField DataField="ENDLUNCH" HeaderText="LUNCH BACK" ApplyFormatInEditMode="Tru
<asp:TemplateField HeaderText="Pto Detail">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("COMPANYPTO") %>'>
<asp:ListItem>NONE</asp:Li
<asp:ListItem>PTO</asp:Lis
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PTOPAID" HeaderText="PTO HR" ReadOnly="True" ApplyFormatInEditMode="Tru
<asp:BoundField DataField="PTOUNPAID" HeaderText="PTO UnPaid" ReadOnly="True" ApplyFormatInEditMode="Tru
<asp:TemplateField HeaderText="Company PTO" >
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("COMPANYPTO") %>'>
<asp:ListItem>NONE</asp:Li
<asp:ListItem>Paid Holiday</asp:ListItem>
<asp:ListItem>Bereviament<
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="COMPANYPTOPAID"
<asp:BoundField DataField="SYMITARCODE" HeaderText="ESPY CODE" ReadOnly="True" ApplyFormatInEditMode="Tru
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="update" />
<asp:Button ID="Button2" runat="server" Text="cancel" />
<asp:sqldatasource ID="Sqldatasource1" runat="server"
ConnectionString="<%$ ConnectionStrings:wages1SQ
SelectCommand="SELECT [WORKDATE], [STARTDAY], [STARTTIME], [ENDTIME], [STARTLUNCH], [ENDLUNCH], [PTODETAIL], [PTOPAID], [PTOUNPAID], [COMPANYPTO], [COMPANYPTOPAID], [SYMITARCODE] FROM [EmployeeAccumInfo]"></asp
</div>
</form>
</body>
</html>
value is the same as what I am bringing out of database
thanks
gary
Meaning that you're trying to set the selectedvalue of something that's not there.
Use the second approach for now and see what you're pulling from the database.
Also for your list items and note that value can differ from the text:
<asp:ListItem Text="None" Value="None" />
Use the second approach for now and see what you're pulling from the database.
Also for your list items and note that value can differ from the text:
<asp:ListItem Text="None" Value="None" />
Replace your field with the field you need to bind from Sqldatasource1
Resource:
http://weblogs.asp.net/gurusarkar/how-to-set-selectedvalue-of-dropdownlist-in-a-gridview