using a datagrid in asp.net

G

Guest

I have a datagrid that I am trying to add rows to on a page. I have several
textboxes that allow data entry for each column in the data grid and a button
that submits the data by refilling the datagrid's datasource (after the new
data is saved in the database) and databinding the datagrid. When I place a
breakpoint at the method used for filling the datasource and walk through the
code, the code works and the datagrid displays the new row of data. However,
when I remove the breakpoint and execute the code without any stops, the
datagrid does not display the new row. Any suggestions on what may be going
on here?
Thanks in advance
Lance
 
G

Guest

I update the database table and then call FillPrintData() and then
BindDataGrid().

private void BindDataGrid()
{
dgPrints.CurrentPageIndex =
Convert.ToInt32(Session["PageIndex"]);
dgPrints.DataSource = (DataTable)Session["DataTable"];
dgPrints.DataBind();
}


private void FillPrintData()
{

string strSQL = "SELECT PrintTable.Print_ID, PrintTable.Photo_ID,
PrintTable.Print_Size, PrintTable.Print_Price, PrintTable.Print_Poster FROM
PrintTable "
+ "WHERE PrintTable.Photo_ID =" + Photo_ID
+ " ORDER BY Print_ID Desc";
OleDbCommand cmd;
OleDbConnection cnn = null;
OleDbDataReader dr = null;
dt = new DataTable();
DataRow darow;
//try
//{
cnn = new OleDbConnection(
ConfigurationSettings.AppSettings["AccessConnectionString"]);
cnn.Open();
cmd = new OleDbCommand(strSQL, cnn);
dr = cmd.ExecuteReader();



// Fill in data.
dt.Columns.Add(new DataColumn(
"C0", typeof(string )));
dt.Columns.Add(new DataColumn(
"C1", typeof(string )));
dt.Columns.Add(new DataColumn(
"C2", typeof(string )));
dt.Columns.Add(new DataColumn(
"C3", typeof(double )));
dt.Columns.Add(new DataColumn(
"C4", typeof(string )));
// Loop through all the rows, retrieving the
// columns you need. Also look into the GetString
// method (and other get... methods) for a faster
// way to retrieve individual columns.
while (dr.Read())
{
darow = dt.NewRow();
darow[0] = dr[0].ToString();
darow[1] = dr[1].ToString();
darow[2] = dr[2].ToString();
darow[3] = dr[3].ToString();
darow[4] = dr[4].ToString();
dt.Rows.Add(darow);

//lblError.Text = "This";
//lstResults.Items.Add(String.Format("{0} {1}: {2}",
// dr[0].ToString(), dr[1].ToString(), dr[2].ToString()));
}
Session["DataTable"] = dt;
//mdv = new DataView(dt);
//return mdv;
}
 
W

W.G. Ryan eMVP

Primarily it looks like you aren't closing the connection - very bad. If
you're trying to fill a datatable, why not just call
DataAdapter.Fill(DataTable);?For what you're doing, you get 0 benefit by
using a reader and you have to write a lot more code. Also, you really
ought ot paramaterize that query... it's better in every respect.

Let me know if either setting the Closeconnection enum in your call to
executeReader or adding a close in a try/catch/finally doesn't fix it.
 
G

Guest

W.G.
Thanks for your response. I closed the connection on the Reader like you
mentioned and I still am having the same results. (I Actually had the
Try/Catch/Finally which closed the objects commented out in my original
code). I then changed the code to include in my dataset the table being used
to populate the grid and then used the .fill method (instead of the Reader).
Still the same results. Both scenarios will work if I walk though the code,
but if I run it without breaking, the grid does not refresh with the new Row.
Very confusing.
Also, What do you mean by "paramaterize that query"? Example
Thanks Again
Lance

W.G. Ryan eMVP said:
Primarily it looks like you aren't closing the connection - very bad. If
you're trying to fill a datatable, why not just call
DataAdapter.Fill(DataTable);?For what you're doing, you get 0 benefit by
using a reader and you have to write a lot more code. Also, you really
ought ot paramaterize that query... it's better in every respect.

Let me know if either setting the Closeconnection enum in your call to
executeReader or adding a close in a try/catch/finally doesn't fix it.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Lance said:
I update the database table and then call FillPrintData() and then
BindDataGrid().

private void BindDataGrid()
{
dgPrints.CurrentPageIndex =
Convert.ToInt32(Session["PageIndex"]);
dgPrints.DataSource = (DataTable)Session["DataTable"];
dgPrints.DataBind();
}


private void FillPrintData()
{

string strSQL = "SELECT PrintTable.Print_ID, PrintTable.Photo_ID,
PrintTable.Print_Size, PrintTable.Print_Price, PrintTable.Print_Poster FROM
PrintTable "
+ "WHERE PrintTable.Photo_ID =" + Photo_ID
+ " ORDER BY Print_ID Desc";
OleDbCommand cmd;
OleDbConnection cnn = null;
OleDbDataReader dr = null;
dt = new DataTable();
DataRow darow;
//try
//{
cnn = new OleDbConnection(
ConfigurationSettings.AppSettings["AccessConnectionString"]);
cnn.Open();
cmd = new OleDbCommand(strSQL, cnn);
dr = cmd.ExecuteReader();



// Fill in data.
dt.Columns.Add(new DataColumn(
"C0", typeof(string )));
dt.Columns.Add(new DataColumn(
"C1", typeof(string )));
dt.Columns.Add(new DataColumn(
"C2", typeof(string )));
dt.Columns.Add(new DataColumn(
"C3", typeof(double )));
dt.Columns.Add(new DataColumn(
"C4", typeof(string )));
// Loop through all the rows, retrieving the
// columns you need. Also look into the GetString
// method (and other get... methods) for a faster
// way to retrieve individual columns.
while (dr.Read())
{
darow = dt.NewRow();
darow[0] = dr[0].ToString();
darow[1] = dr[1].ToString();
darow[2] = dr[2].ToString();
darow[3] = dr[3].ToString();
darow[4] = dr[4].ToString();
dt.Rows.Add(darow);

//lblError.Text = "This";
//lstResults.Items.Add(String.Format("{0} {1}: {2}",
// dr[0].ToString(), dr[1].ToString(), dr[2].ToString()));
}
Session["DataTable"] = dt;
//mdv = new DataView(dt);
//return mdv;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top