GridView not displaying data in browser on pressing button Go when bind to DataTable

W

weird0

I have created a GridView and dynamically added data to it by
creating a DataTable(as advised) and bound it with a reader. Then,
assigned the DataSource of GridView to DataTable. But even that aint
working as alternatively


assigned reader obj. to datasource of GridView. Here is the code for
the aspx.cs page: ( The reader surely cootains data as its HasRows
property is true). Plz help. Never got stuck this bad with a problem.
For more than two weeks.

protected void Button1_Click(object sender, EventArgs e)
{
// CustID->AccId->ATM-Id->Atm_TransID....
int AtmtransId=-1;
if (Session["CustomerId"]!=null)

AtmtransId=DataAccessLayer.ATMManager.getATMTransactionsID(Session["CustomerId"].ToString(),AccNamesList.SelectedItem.Text);
string Query = "SELECT atmtrans_date AS Date,atmtrans_branch
AS Branch,atmtrans_type AS Type,atmtrans_amount AS Amount FROM
ATM_Transactions WHERE atmtrans_id=@ATMTRANSID";
SqlConnection sqlconnection = new SqlConnection(@"Data Source=.
\SQLEXPRESS;AttachDbFilename=|DataDirectory|\BankingDb.mdf;Integrated
Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand(Query,sqlconnection);
cmd.Parameters.AddWithValue("@ATMTRANSID",AtmtransId);
GridView GridView1 = new GridView();
sqlconnection.Open();
// Fill the Data Reader
SqlDataReader reader=cmd.ExecuteReader();
//Create a DataTable object
DataTable dt=new DataTable();
//Load Data from Data Reader to DataTable object
dt.Load(reader);
//Fill the GridView Control
GridView1.DataSource = dt;
GridView1.DataBind();
reader.Close();
sqlconnection.Close();
}
 
G

Guest

Couple suggestions:
1) Try the ASP.NET group, this is really not a C# language group question.
2) Get in the habit of wiring up any code you write that could *possibly*
cause an exception into a try / catch / finally block pattern.

When you do this, for debugging purposes you can do things like:

System.Diagnostics.Debug.WriteLine( ex.Message +ex.StackTrace);

on the "ex" Exception object inside the catch block and get a lot of very
useful information about what went wrong.

Peter
 

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