Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i post my bind method in form load event it shows cannot find table 0 error.
also when i bind single combo box its working but when i bind more than one combo box its shows error.

What I have tried:

DAL part is.............
C#
public DataSet prno_combo(SqlCommand cmd)
        {
            DataSet ds = new DataSet();

           
                getcon();
                cmd.Connection = getcon();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                adapt.Fill(ds, "Purchase");
                DataRow dr = ds.Tables[0].NewRow();
                dr["Id"] = "Select";
                dr["Id"] = "0";
                ds.Tables[0].Rows.InsertAt(dr, 0);                             
                return ds;

}

 public DataSet store_combo(SqlCommand cmd)
        {
            DataSet ds = new DataSet();
            
                                 
                        getcon();
                        cmd.Connection = getcon();
                        SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                        adapt.Fill(ds, "StoreMaster");
                        DataRow dr = ds.Tables[0].NewRow();
                        dr["StoreName"] = "Select";
                        dr["id"] = "0";
                        ds.Tables[0].Rows.InsertAt(dr, 0);
                        return ds;
                }

BL part is..............


C#
public DataSet BL_prno_combo(clsPRml mlprobj)
       {
           SqlCommand cmd = new SqlCommand();
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "Prc_BindPurchaseMandate";
           return dlprobj.prno_combo(cmd);
       }

       public DataSet BL_store_combo(clsPRml mlprobj)
       {
           SqlCommand cmd = new SqlCommand();
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "Prc_BindStore";
           return dlprobj.store_combo(cmd);

Form of presentation part is......
C#
public void BindPRnoCombo()
       {

               cbPRNo.DataSource = blprobj.BL_prno_combo(mlprobj).Tables[0];
               cbPRNo.ValueMember = "Id";
               cbPRNo.DisplayMember = "Id";
               cbPRNo.SelectedIndex = 0;


       }


       public void BindStoreCombo()
       {

               // ERROR OCCURS HERE
               cbStore.DataSource = blprobj.BL_store_combo(mlprobj).Tables[0];
               cbStore.ValueMember = "Id";
               cbStore.DisplayMember = "StoreName";
               cbStore.SelectedIndex = 0;
Posted
Updated 11-Dec-16 23:28pm
v5
Comments
preety sunita 12-Dec-16 1:55am    
not getting the question..please give more information or code regarding it.
SahuA 12-Dec-16 3:10am    
I m trying to bind combo box using three tier architecture, when i bind first combo box its working but when i bind the second combo box and placed the method name on load event it shows the "Cannot find table 0."
Andy Lanng 12-Dec-16 4:41am    
Could you show us which line the error occurs on. You use Table[0] often.
PS: It's not a great idea to always assume that there will be a table[0]. You should handle the case of getting no table back.
SahuA 12-Dec-16 5:19am    
Error occurs at this line...........

cbStore.DataSource = blprobj.BL_store_combo(mlprobj).Tables[0];
preety sunita 12-Dec-16 5:15am    
Please try to debug your DAL layer with F11,i think there must be an issue and check the ds has tables or not in form representation code...

Dataset ds = new Dataset();
ds =blprobj.BL_store_combo(mlprobj);
if(ds.tables.count > 0) // check here is the count of table.
{
----------Your code ----------

}

1 solution

Always make a practice to validate a DataTable or DataSet wherever you are using it.

C#
if(ds!=null && ds.Tables.Count > 0){

//code here

}


or
C#
if(dt!=null && dt.Rows.Count > 0){

//code here

}


Let me know for further assistance if required.
 
Share this answer
 
Comments
SahuA 12-Dec-16 6:21am    
when i placed this command to check the table and rows it didn't gives any error but also does not show any dropdown in second combo box,
Also when i removed the method of first combobox from load event the dropdown appears in the second combobox.
Nirav Prabtani 13-Dec-16 2:33am    
you have to debug your code.
Put a break point and check data is there or not.

If you dont have data in your datatable. How can you bind you controls ?

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