Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I am using following simple statements in my cs file and getting error
"cannot find table 0"...

Unable to understand why..?
I have one table in my database
login(name,pwd)...
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            a = 0;
        con = new SqlConnection();
        myHostName = Dns.GetHostName().ToString();
       
        con.ConnectionString = ConfigurationManager.AppSettings["SqlConnectionString"];
        sql = "select * from login";
        adp = new SqlDataAdapter(sql, con);
        ds = new DataSet();
        try
        {
            adp.Fill(ds);
        }
        catch (Exception ex) { }
        int i;
      
       // Label3.Text = "asdfasd" + ds.Tables.Count.ToString();
        bool flag=false;
        if (a <= 2)
        {
            if (ds.Tables[0].Rows.Count > 0)//this line is shwoing error
            {
                for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if ((txtLName.Text == ds.Tables[0].Rows[i]["name"].ToString()) && (txtPwd.Text == ds.Tables[0].Rows[i]["pwd"].ToString()))
                    {
                        flag = true;
                        goto b;
                    }
                    else
                    {
                        flag = false;
                    }

                }
            }
        }
        else
        {
            Label3.Text = "Matched 3 times";
            Button1.Enabled = false;
        }
Posted
Updated 11-Jul-17 15:01pm
v2

C#
if (ds.Tables[0].Rows.Count > 0)

This line showing error before this line you must check the count of table present in your data set i.a.
C#
if(ds.Tables.count > 0)
{

if (ds.Tables[0].Rows.Count > 0)
.
.
.

}

Hope It's Working Fine.. :)
 
Share this answer
 
v2
Line showing error should be like

if(ds.Tables.Count >0 && ds.Tables[0].Rows.Count > 0)
{
.
.
.



}
 
Share this answer
 
Comments
satyajeett 29-Mar-13 6:43am    
thanks to both of you..but if the index is starting from 0 ?.. or if i use ds.Tables.count > 0 condition then how can i use tables[0].row.count.. its going to be always False...!!
Dnyaneshwar Kondbale 29-Mar-13 6:57am    
Actully ds.Tables.count is check the no of table present in DataSet, If In my DataSet there is 0 table present(means DataSet is null) then How can we perform the operation like Rows.count on Table which is at 0 position,
In ds.Tables.count > 0 line we check how many tables if it is greater than zero does not mean We need to check Ds.Tables[1] its always start from Ds.Tables[0].
:)
satyajeett 29-Mar-13 7:19am    
hey thanks for explanation but plz look again at my code..as i said i ve 1 table in my database... the condition (ds.tables.count>0) is always returning false.
Dnyaneshwar Kondbale 29-Mar-13 7:36am    
Hii Satya,
Here In dataset there is only one table as you say.
Then Ds.Table.count must return table count i.e. 1 the Count property return the count of table i.e. 1 and its not return the index of table i.e. 0.

There is diff between Index and count.
0 is index and 1 is count in above condition.

:)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Admin_JobClassMaster : System.Web.UI.Page
{
    GenFun objgen = new GenFun();
    DB objcon = new DB();
    static DataSet ds = new DataSet();

    static string name;
  
        protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            if (!IsPostBack)
            {
               
            }
            if (Request.Cookies["Name"] != null)
            {

                name = Request.Cookies["Name"].Value.ToString();
               // Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);

                if ((((name)) != "0") && (((name)) != ("EMPINFO")))
                {
                    btnDelete.Visible = true;
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                   
                    if (!IsPostBack)
                    {

                        FetchDataByID();
                        lblStatus.Text = "";
                    }




                }
                else
                {
                    if ((((name)).Equals("EMPINFO")))
                        lblStatus.Text = "<div class=\"alert alert-success alert-dismissable\"><b>Success:</b> New Employee Inserted Successfully. </div>"; 
                    btnDelete.Visible = false;
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                   
                }
            }
            else
            {
                btnDelete.Visible = false;
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
                
                lblStatus.Text = "";

            }
           
        }
        catch (SqlException ex1)
        {
            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null){ Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);}
        }
        catch (Exception ex1)
        {

            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null){ Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);}

        }
    }
    void FetchDataByID()
    {



        List<object> lsobj = new List<object>();
        lsobj.Add(((name)));
        lsobj.Add(Session["companyid"].ToString());
        ds = objcon.FetchData(lsobj, "HR_JobClassMaster_SP", "SELECTBYID");
        if (ds.Tables[0].Rows.Count > 0)
        {
            

                TxtJobClassTitle.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString();
                TxtStartRange.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
                TxtMidRange.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
                TxtHighRange.Text = ds.Tables[0].Rows[0].ItemArray[4].ToString();
                DrpBouns.SelectedIndex = DrpBouns.Items.IndexOf(DrpBouns.Items.FindByValue(ds.Tables[0].Rows[0].ItemArray[5].ToString()));
                DrpCommission.SelectedIndex = DrpCommission.Items.IndexOf(DrpCommission.Items.FindByValue(ds.Tables[0].Rows[0].ItemArray[6].ToString()));
                DrpBenifits.SelectedIndex = DrpBenifits.Items.IndexOf(DrpBenifits.Items.FindByValue(ds.Tables[0].Rows[0].ItemArray[7].ToString()));
                TxtWorkHours.Text = ds.Tables[0].Rows[0].ItemArray[8].ToString();
          
        }

    }
    void CRUDINSUPDLT(string Action)
    {

        List<object> lsobj = new List<object>();
        if ((name) != null && name != "")
        {
            if (((name)) != "0")
            {
                lsobj.Add(((name)));

            }
        }



        lsobj.Add(TxtJobClassTitle.Text);
        lsobj.Add("0");
        lsobj.Add("0");
        lsobj.Add("0");
        lsobj.Add(DrpBouns.SelectedItem.ToString());
        lsobj.Add(DrpCommission.SelectedItem.ToString());
        lsobj.Add(DrpBenifits.SelectedItem.ToString());
        lsobj.Add(TxtWorkHours.Text);
        lsobj.Add(Session["branchid"].ToString());
        lsobj.Add("N");
        lsobj.Add(Session["username"].ToString());
        lsobj.Add(System.DateTime.Now.ToString());
        lsobj.Add(Session["companyid"].ToString());



        objcon.insupdlt(lsobj, "HR_JobClassMaster_SP", Action);


        objgen.createlog(Session["branchid"].ToString(), Session["username"].ToString(), HttpContext.Current.Request.Url.AbsolutePath, "HR_JobClassMaster_SP", ds, lsobj, Action);




    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {



            CRUDINSUPDLT("INSERT");
            HttpCookie cName = new HttpCookie("Name");
            cName.Value = ("INSERT1");
            Response.Cookies.Add(cName);
            Response.Redirect("DisplayJobClass.aspx", false);



            lblStatus.Text = "";
        }
        catch (SqlException ex1)
        {
            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1); }
        }
        catch (Exception ex1)
        {

            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1); }

        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {



            CRUDINSUPDLT("UPDATE");
            HttpCookie cName = new HttpCookie("Name");
            cName.Value = ("UPDATE");
            Response.Cookies.Add(cName);
            Response.Redirect("DisplayJobClass.aspx", false);

            lblStatus.Text = "";
        }
        catch (SqlException ex1)
        {
            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1); }
        }
        catch (Exception ex1)
        {

            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1); }

        }

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {

            HttpCookie cName = new HttpCookie("Name");
            cName.Value = ("DELETE");
            Response.Cookies.Add(cName);

            CRUDINSUPDLT("DELETE");
            Response.Redirect("DisplayJobClass.aspx", false);

            lblStatus.Text = "";
        }
        catch (SqlException ex1)
        {
            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1); }
        }
        catch (Exception ex1)
        {

            lblStatus.Text = "<div class=\"alert alert-danger alert-dismissable\">Error: " + ex1.Message + " </div> "; if (Request.Cookies["Name"] != null) { Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1); }

        }
    }
  
}
 
Share this answer
 

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