Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the source code which i have used

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Asp.net Bind Data to Dropdownlist in inside of gridview</title>
 <style type="text/css">
        .TableCSS
        {
            border-style:none;
            background-color:Gray;
            width: 600px;
            }
        .TableHeader
        {
            background-color:Gray;
            color:White;
            font-size:large;
            font-family:Verdana;
            height:45px;
            }
        .TableData
        {
            background-color:Silver;
            color:White;
            font-family:Courier New;
            font-size:medium;
            font-weight:bold;
            height:30px;
            }
        .TablePager
        {
            background-color:Gray;

            height:50px;
            }
        .ButtonCSS
        {
            color:Black;
            height:35px;
            font-weight:bold;
            }
        .NumericButtonCSS
        {
            font-size:x-large;
            font-family:Courier New;
            color:White;
            font-weight:bold;
            }
        .CurrentPageLabelCSS
        {
            font-size:xx-large;
            font-family:Courier New;
            color:White;
            font-weight:bold;
            }
        .NextPreviousButtonCSS
        {
            font-size:large;
            font-family:Courier New;
            color:White;
            font-weight:bold;
            }
    </style>
</head>
<body>
<form id="form1" runat="server">
  <div>
        <h2 style="color:Navy; font-style:italic;">Smart Pin</h2>
        <hr width="575" align="left" color="PowderBlue" />
        <asp:SqlDataSource
            ID="SqlDataSource1"
            runat="server"
            ConnectionString="<%$ ConnectionStrings:sqlCon %>"
            SelectCommand="SELECT smartpin FROM Smart_id WHERE is_activate=1"
            >
        </asp:SqlDataSource>
        <br />
        <asp:ListView
            ID="gvUserInfo"
            runat="server"
            DataSourceID="SqlDataSource1"
            ondatabound="gvUserInfo_DataBound">
            <LayoutTemplate>
                <table id="Table1" runat="server" class="TableCSS">
                    <tr id="Tr1" runat="server" class="TableHeader">
                        <td id="Td1" runat="server">Smart Pin</td>
                         <td id="Tdselect" runat="server">Select Smart Pin</td>
                          <td id="Tdsponcorid" runat="server">Sponcor Id</td>
                    </tr>
                    <tr id="ItemPlaceholder" runat="server">
                    </tr>
                    <tr id="Tr2" runat="server" class="TablePager">
                        <td id="Td3" runat="server" colspan="2">
                            <asp:DataPager ID="DataPager1" runat="server">
                                <Fields>
                                    <asp:NextPreviousPagerField
                                        ButtonType="Button"
                                        ShowFirstPageButton="true"
                                        ShowNextPageButton="true"
                                        ShowPreviousPageButton="false"
                                        ButtonCssClass="ButtonCSS"
                                        />
                                    <asp:NumericPagerField
                                        NumericButtonCssClass="NumericButtonCSS"
                                        CurrentPageLabelCssClass="CurrentPageLabelCSS"
                                        NextPreviousButtonCssClass="NextPreviousButtonCSS"
                                        />
                                    <asp:NextPreviousPagerField
                                        ButtonType="Button"
                                        ShowLastPageButton="true"
                                        ShowNextPageButton="false"
                                        ButtonCssClass="ButtonCSS"
                                        />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr class="TableData">
                    <td>
                        <asp:Label
                            ID="Label1"
                            runat="server"
                            Text='<%# Eval("smartpin")%>'>
                        </asp:Label>
                    </td>
                  <td>
                  <asp:CheckBox ID="chksmart" runat="server" />

                  </td>
                  <td>
                  <asp:DropDownList ID="ddsponcor" runat="server" Width="100px" OnClick="ddsponcor_SelectedIndexChanged"/>
                  </td>
                </tr>
            </ItemTemplate>


      </asp:ListView>
    </div>
</form>
</body>
</html>



//and this code behind which i have used

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class sendpin : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SHILPA;Initial Catalog=TreeDB;Integrated Security=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridview();
        }
    }
    // This method is used to bind gridview from database
    protected void BindGridview()
    {
        
                //ddl.Items.Insert(0, new ListItem("--Select--", "0"));
        DataTable dt = new DataTable();

        DropDownList ddl = new DropDownList();

        dt = DAL.Registration_Master.View();

        ddl.DataSource = dt;
        ddl.DataValueField = dt.Columns["sponcorid"].ToString();
        ddl.DataTextField = dt.Columns["sponcorid"].ToString();
        ddl.DataBind();

                        

    }

   
}

// i am not getting values which i have fetched from database in dropdownlist, please tell me where i am go wrong.
Posted
Updated 1-Dec-12 20:34pm
v2
Comments
AmitGajjar 2-Dec-12 2:35am    
is your datatable dt filled with data ?

1 solution

As Amith suggested make sure the dataTable has data. Also try replacing the below lines

C#
ddl.DataSource = dt;
ddl.DataValueField = dt.Columns["sponcorid"].ToString();
ddl.DataTextField = dt.Columns["sponcorid"].ToString();

with
C#
ddl.DataSource = dt;
ddl.DataValueField = "sponcorid";
ddl.DataTextField = "sponcorid";
 
Share this answer
 
Comments
sumit kausalye 2-Dec-12 9:23am    
it doesn't work still
__TR__ 2-Dec-12 9:27am    
I see you have given "ddsponcor" as Id of the drop down but you are using ddl as Id in code behind. The Id in aspx and code behind file should be the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900