简体   繁体   中英

not able to use two dataset on same .aspx.cs page

I am using two Datasets on this page 1.DsCountry(in BindCountry()) and 2.DsState(in BindState()) but data from the database is getting filled only in DsCountry not in DsState but when BindCountry() is commented DsState is working.

public void BindCountry()
{
    objCountry.SortOn = Convert.ToString(ViewState["SortOn"]);
    objCountry.SortBy = Convert.ToString(ViewState["SortBy"]);
    DataSet DsCountry = objCountry.GetAllCountryDetails();
    ddlCntry.DataSource = DsCountry.Tables[0];
    ddlCntry.DataValueField = "CountryId";
    ddlCntry.DataTextField = "CountryName";
    ddlCntry.DataBind();
}

public void BindState()
{
    objState.CountryId = int.Parse(ddlCntry.SelectedItem.Value);
    objState.StateName = txtState.Text.Trim();
    objState.SortOn = ViewState["SortOnState"].ToString();
    objState.SortBy = ViewState["SortByState"].ToString();
    DataSet DsState = objState.getStates();
    gvState.DataSource = DsState;
    gvState.DataBind();

}

In here:

public void BindState()
{
    objState.CountryId = int.Parse(ddlCntry.SelectedItem.Value);
    objState.StateName = txtState.Text.Trim();
    objState.SortOn = ViewState["SortOnState"].ToString();
    objState.SortBy = ViewState["SortByState"].ToString();
    DataSet DsState = objState.getStates();
    gvState.DataSource = DsState;
    gvState.DataBind();
}

on this line:

gvState.DataSource = DsState;

if this is a dataset (implies it has multiple tables in it), then you need to specify a specific table you want to bind to, just like in your country method:

ddlCntry.DataSource = DsCountry.Tables[0];

will become this:

gvState.DataSource = DsState.Tables[0];

Assuming you return only one table in the dataset.

otherwise its like: go get me something I do not know what. :)

Your BindState Code should be like this

public void BindState() {

objState.CountryId = int.Parse(ddlCntry.SelectedItem.Value);
objState.StateName = txtState.Text.Trim();
objState.SortOn = ViewState["SortOnState"].ToString();
objState.SortBy = ViewState["SortByState"].ToString();
DataSet DsState = objState.getStates();
gvState.DataSource = DsState.Tables[0];
gvState.DataBind();

}

You can apply more than 1 dataset in 1 page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM