0

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?

2
  • Are you getting value in cTable? Commented Jul 19, 2015 at 17:27
  • "Using Debugging" . ..1.Keep a Debug Point at "Dataset Company" 2. Run the Application .. Execution will hold at that point and you can see the Value in c Table Commented Jul 21, 2015 at 6:19

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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");
            }


        } 

1 Comment

Well, if cTable is, for some reason null, what's will happen with Your code if(cTable.Rows.Count >0) ?? I think some error will rise. So, I think it's better to replace that code with if (Company != null && Company.Tables[0] != null) { cTable = Company.Tables[0]; ... and rest of Your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.