?
Solved

How do I bind a field from a sql server to a dropdownlist

Posted on 2014-09-12
5
Medium Priority
?
194 Views
Last Modified: 2014-09-19
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="SqlDataSource1">  
            <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:ListItem>
                            <asp:ListItem>PTO</asp:ListItem>
                            </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:ListItem>
                            <asp:ListItem>Paid Holiday</asp:ListItem>
                            <asp:ListItem>Bereviament</asp:ListItem>
                            </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:wages1SQLConnectionString %>"
            SelectCommand="SELECT [WORKDATE], [STARTDAY], [STARTTIME], [ENDTIME], [STARTLUNCH], [ENDLUNCH], [PTODETAIL], [PTOPAID], [PTOUNPAID], [COMPANYPTO], [COMPANYPTOPAID], [SYMITARCODE] FROM [EmployeeAccumInfo]"></asp:sqldatasource>
    </div>
0
Comment
Question by:javagair
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40319604
<asp:DropDownList ID="DropDownList1" DataTextField="PTO"  runat="server" SelectedValue='<%# Bind("<YOUR FIELD>") %>'>

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
0
 

Author Comment

by:javagair
ID: 40320134
this one gives error
<asp:TemplateField HeaderText="Pto Detail">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue="<%# Bind("PTODETAIL") %>">
                           
                            <asp:ListItem>NONE</asp:ListItem>
                            <asp:ListItem>PTO</asp:ListItem>
                            </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:ListItem>
                            <asp:ListItem>Paid Holiday</asp:ListItem>
                            <asp:ListItem>Bereviament</asp:ListItem>
                            </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
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 40320191
From the resource as well:

you can do the following:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType== DataControlRowType.DataRow)
    {
        DataRowView drv = e.Row.DataItem as DataRowView;
        DropDownList ddl1 = e.Row.FindControl("DropDownList1") as DropDownList;
        if(ddl1 != null)
        {
            //Get the data from DB and bind the dropdownlist
            //you may need to double check to ensure you have a value.
             if (string.IsNullOrEmpty(drv["PTODETAIL"].ToString())
                //set a default
              else
                 ddl1.SelectedValue = drv["PTODETAIL"].ToString();
        }
    }
}

Open in new window

0
 

Author Comment

by:javagair
ID: 40320226
ACTUALLY NEITHER WORK


this is the runtime error that occurs:
[ArgumentOutOfRangeException: '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="SqlDataSource1">  
            <Columns>  
                <asp:BoundField DataField="WORKDATE" HeaderText="DATE" ReadOnly="True"  ApplyFormatInEditMode="False" />                  
                <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:RequiredFieldValidator runat="server" ID="rfStartTime"  ControlToValidate="StartTime" ValidationGroup="var1" ErrorMessage=" " />

            </EditItemTemplate>

         </asp:TemplateField>
               
                <asp:BoundField DataField="ENDTIME" HeaderText="OUT"  ReadOnly="True"  ApplyFormatInEditMode="True" />
                     
                <asp:BoundField DataField="STARTLUNCH" HeaderText="LUNCH" ReadOnly="True"  ApplyFormatInEditMode="True" />
                   
                <asp:BoundField DataField="ENDLUNCH" HeaderText="LUNCH BACK"  ApplyFormatInEditMode="True" />  
               
                 <asp:TemplateField HeaderText="Pto Detail">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("COMPANYPTO") %>'>
                           
                            <asp:ListItem>NONE</asp:ListItem>
                            <asp:ListItem>PTO</asp:ListItem>
                            </asp:DropDownList>
                    </ItemTemplate>
                   
            </asp:TemplateField>
           
                 
               
                <asp:BoundField DataField="PTOPAID" HeaderText="PTO HR" ReadOnly="True"  ApplyFormatInEditMode="True" />  
                  <asp:BoundField DataField="PTOUNPAID" HeaderText="PTO UnPaid" ReadOnly="True"  ApplyFormatInEditMode="True" />    
               
                  <asp:TemplateField HeaderText="Company PTO" >
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("COMPANYPTO") %>'>
                            <asp:ListItem>NONE</asp:ListItem>
                            <asp:ListItem>Paid Holiday</asp:ListItem>
                            <asp:ListItem>Bereviament</asp:ListItem>
                            </asp:DropDownList>
                    </ItemTemplate>
                   
            </asp:TemplateField>  
                <asp:BoundField DataField="COMPANYPTOPAID" HeaderText="COMPANY HRS" ReadOnly="True"  ApplyFormatInEditMode="True" />  
                   <asp:BoundField DataField="SYMITARCODE" HeaderText="ESPY CODE" ReadOnly="True"  ApplyFormatInEditMode="True" />
               
         
            </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:wages1SQLConnectionString %>"
            SelectCommand="SELECT [WORKDATE], [STARTDAY], [STARTTIME], [ENDTIME], [STARTLUNCH], [ENDLUNCH], [PTODETAIL], [PTOPAID], [PTOUNPAID], [COMPANYPTO], [COMPANYPTOPAID], [SYMITARCODE] FROM [EmployeeAccumInfo]"></asp:sqldatasource>
    </div>
    </form>
</body>
</html>

value is the same as what I am bringing out of database

thanks

gary
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40320279
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" />
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question