开发者

DataGrid.Items filled with Null data

开发者 https://www.devze.com 2023-04-10 00:54 出处:网络
I\'m trying to fill a asp.DataGrid with data from a DB2 database.The problem I\'m getting is that the data is coming back null.The weird thing is, the records themselves (seem to) load from the databa

I'm trying to fill a asp.DataGrid with data from a DB2 database. The problem I'm getting is that the data is coming back null. The weird thing is, the records themselves (seem to) load from the database, as when I step through the code while debugging, the DataGrid.Items.Count = the number of records I have in the database itself.

Additionally, while troubleshooting, I've added an asp.Label that's initially hidden to display the number of records found in the DataGrid itself, and each time it displays the correct number of records.

Here's my code:

protected void Page_Load(object sender, EventArgs e)
{
    dta_grd = new DataGrid();
    dta_grd = Ex_DLL.GetData("select * from tstint/m02");
    Lbl_Dsply.Visible = true;
    if (Supp_Data.Items.Count == 0)
    {
        Lbl_Dsply.Text = "No Records Found!";
    }
    else
    {
        Lbl_Dsply.Text += dta_grd.Items.Count.ToString();
    }
}

Ex_DLL is simply the name of a library that does all the connections to the Database itself.

The Code for Ex_DLL.GetData():

public static DataGrid GetData(string str_sql)
{
    //EX_Global.DB2_Conn("DEV") is just an internal connection function that houses
    //the connection settings.      
    iDB2Connection db2_conn = EX_Global.DB开发者_StackOverflow2_Conn("DEV");
    iDB2Command db2_cmd = null;
    iDB2DataAdapter db2_adpt = null;
    DataSet dta_ds = new DataSet();
    DataGrid ret_val = new DataGrid();
    try
    {
        if (db2_conn.State != System.Data.ConnectionState.Open) { db2_conn.Open(); }
        db2_cmd = new iDB2Command(str_sql, db2_conn);
        db2_adpt = new iDB2DataAdapter(db2_cmd);
        db2_adpt.Fill(dta_ds);
        ret_val.DataSource = dta_ds;
        ret_val.DataBind();
        db2_conn.Close();
    }
    catch (Exception) { }
    return ret_val; 
}

Now, when I read them individually using idb2DataReader, it's actually reading from the database, but there's just something lost in translation from reading the database to filling the DataGrid itself.

Any ideas?


There are two problems (may be more) I can see:

  1. You instantiate the DataGrid control during page execution but forget to add it to page's Controls collection.
  2. You missed Datasource property and DataBind() method.

In design environment, add a "PlaceHolder" control on a page (.aspx) (Say PlaceHolder1)

protected void Page_Load(object sender, EventArgs e)
{
    dta_grd = new DataGrid();
    dta_grd.DataSource = Ex_DLL.GetData("select * from tstint/m02");
    dta_grd.DataBind(); // this method populates the DataGrid from assigned datasource
    PlaceHolder1.Controls.Add(dta_grd);

    Lbl_Dsply.Visible = true;
    if (Supp_Data.Items.Count == 0)
    {
        Lbl_Dsply.Text = "No Records Found!";
    }
    else
    {
        Lbl_Dsply.Text += dta_grd.Items.Count.ToString();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号