OverflowStack
OverflowStack

Reputation: 909

Binding Data to DropDownList in ASP.NET

I'm making a website inside the Visual Studio using asp.net. My website is connected to Web Serbice Server which is connected to SQL database.

On my mainpage.aspx, at the top of the file, I have the first following line:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="mainpage.aspx.cs"
Inherits=WebApplication.mainpage" %>

Later in the same mainpage.aspx file I have a dropdown list:

<asp:DropDownList ID="DropDownList1" runat="server" width="140px"></asp:DropDownList>

In my mainpage.aspx.cs file I'm writing the following code inside the Pade_Load() :

DataSet Company = WebSerivce.Company(IP, DataBaseName, UserName, Password);
DataTable cTable = Company.Table[0];

if(!Page.IsPostBack)
{
DropDownList1.DataSource = cTable;
DropDownList1.DataValueField = "ID_Comp";
DropDownList1.DataTextField = "Comp_Name";
DropDownList1.DataBind();
} 

SQL DataBase Table:

ID_Comp     | int          | (Primary Key)
----------------------------
Comp_Name   | nvarchar(50) |

What am I doing wrong? I've followed the instructions of multiple tutorials but nothing so far has worked. Is there something fundamental I'm missing?

Upvotes: 0

Views: 3800

Answers (2)

Raj Chaurasia
Raj Chaurasia

Reputation: 1974

Please try as below. This way you can find if you are binding empty datatable or not. Based on this we can further troubleshoot.

        DataTable cTable = null;
        DataSet Company = WebSerivce.Company(IP, DataBaseName, UserName, Password);

        if (Company != null && Company.Tables[0] != null)
            cTable = Company.Tables[0];

        if (!Page.IsPostBack)
        {
            if(cTable != null && cTable.Rows.Count > 0)
            {
                DropDownList1.DataSource = cTable;
                DropDownList1.DataValueField = "ID_Comp";
                DropDownList1.DataTextField = "Comp_Name";
                DropDownList1.DataBind();
            }
            else
            {
                DropDownList1.DataSource = new DataTable();
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, "Select");
            }


        } 

Upvotes: 0

jsanalytics
jsanalytics

Reputation: 13188

Make sure cTable has data in it. I tested your code below (commenting out the service part) and using a local Northwind database and it worked just fine. So, again, make sure the service is actually returning valid data into cTable. Place a debugger breakpoint and check the contents of cTable.

//DataSet Company = WebSerivce.Company(IP, DataBaseName, UserName, Password);
//DataTable cTable = Company.Table[0];
string conn = ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString();
SqlDataSource dataSource = new SqlDataSource(conn, "select * from region"); 
DataSourceSelectArguments args = new DataSourceSelectArguments();
DataView view = dataSource.Select(args) as DataView;

DataTable cTable = view.ToTable(); 

if (!Page.IsPostBack)
{
    DropDownList1.DataSource = cTable;
    DropDownList1.DataValueField = "RegionID";
    DropDownList1.DataTextField = "RegionDescription";
    DropDownList1.DataBind();
}

Browser dropdown list result:

enter image description here

Upvotes: 1

Related Questions