C# .NET - System.Data.DataTable does not contain a definition for tables...

Asked By Daniel on 30-Sep-12 11:53 AM
Hi I am using the datatable inside a usercontrol page but I am getting the following error<<System.Data.DataTable does not contain a definition for tables>>I do not understand why because I am always use this in other page and it working.
This is the code I am using,and the line of error is written in red beside it.

protected void Page_Load(object sender, EventArgs e)
    {
        User _User = new User();
              
        if (!IsPostBack)
        {
            ddlMemberType.DataSource = _User.GetUserTypeForSignUp();
            ddlMemberType.DataTextField = "UserType";
            ddlMemberType.DataValueField = "UserTypeId";
            ddlMemberType.DataBind();
        }
        if (HttpContext.Current.Request.Url.ToString().ToLower().ToString() == "myaccount.aspx")
        {
            formtitle.InnerHtml = "My Account";
            _Member = new Member(Convert.ToInt32(Session["UserId"]));
            DataTable dt = _User.GetUserByUserId().Tables[0];//This is the line I am getting the error on the Tables[0]
            DataRow dr = dt.Rows[0];
            if (!IsPostBack)
            {  
               
                txtAddress.Text = _Member.Address;
                txtCompanyName.Text = _Member.CompanyName;
                txtContactNo.Text = _Member.PhoneNo;
                txtDescription.Text = _Member.Description;
                txtEmail.Text = _Member.UserName;
                txtName.Text = _Member.Name;
                TextCountry.Text = _Member.Country;
                txtCity.Text = _Member.City;
                txtWebsite.Text = _Member.Website;
                txtExperience.InnerText = _Member.Experience;


                if (AsyncFileUpload1.HasFile)
                string.Format("<div class=\"imgwrapper\"><img width=\"135\" alt=\"{0}\" src=\"{1}\" /></div>", Resources.Language.NoPreview, HelperFunctions.GetAbsolutePath(this.Page, string.Format("Images/Product/{0}/{1}", dr["ProductId"], dr["NormalImage"])));
                          
                ddlMemberType.SelectedValue = _Member.UserTypeId.ToString();
                ddlRegistrationType.SelectedValue = _Member.RegistrationType.ToString();
                ddlRegistrationType_SelectedIndexChanged(null, null);
                ddlMemberType.Enabled = false;
                ddlRegistrationType.Enabled = false;
                txtEmail.Enabled = false;
                btnSave.Text = Resources.Language.Save ;
                pnlCaptcha.Visible = false;
            }
        }
        else
            formtitle.InnerHtml = "Register on W";
    }
Robbe Morris replied to Daniel on 30-Sep-12 04:01 PM
Does this return a DataSet or a DataTable?

User.GetUserByUserId();
Daniel replied to Robbe Morris on 30-Sep-12 05:27 PM
It is return a dataTable.
Robbe Morris replied to Daniel on 30-Sep-12 05:58 PM
Then why are you attempting access a .Tables collection?  A DataTable does not have a collection of Tables.  Only a DataSet does.

Remove .Tables[0] from your method call.
Jitendra Faye replied to Daniel on 01-Oct-12 01:20 AM
First check that what is the output for this statement-

_User.GetUserByUserId().Tables[0];

It may be possible that it is not returning any dataTable.
Hemanth Kumar replied to Daniel on 01-Oct-12 01:25 AM
Daniel,

If this Method does return a DataSet with no tables in it then you will get that error.

Change your code like this
if(_User.GetUserByUserId().Tables.Count>0)
{
DataTable dt = _User.GetUserByUserId().Tables[0];
}

Goniey N (Mr. G) replied to Daniel on 01-Oct-12 06:43 AM
This error comes because there is nothing return using this "_User.GetUserByUserId().Tables[0]".

So now what?

for that you have to check first of all that is that return anything or not? If return then go ahead otherwise check that why it is not return any table?

You have to write one condition in that and you have to check that does your method return any table or not?


if(_User.GetUserByUserId().Tables.Count > 0) //Here it checks that does any table return or not?
{
            DataTable dt = _User.GetUserByUserId().Tables[0];
            DataRow dr = dt.Rows[0];
            //Your other code....
}



Check this...
Hope this will helpful to you...
Daniel replied to Robbe Morris on 01-Oct-12 03:38 PM
Thanks Morris!
Daniel replied to Robbe Morris on 01-Oct-12 03:39 PM
Thanks Morris!