Reputation: 217
I'm having trouble binding a method to a gridview, could anybody help?.
Basically, i'm returning a dataset in the getAllPatients method
and then passing it to the asp.net page. When I go to load the page nothing happens.
Here is my method:
public DataSet GetAllPatients()
{
//create objects
SqlConnection conn = null;
SqlDataAdapter da = null;
DataSet ds = null;
string sql = string.Empty;
//create sql string
sql = "SELECT * FROM GP_Patient";
//create connection
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GPConn"].ConnectionString);
try
{
//create Data adapter
da = new SqlDataAdapter(sql, conn);
//fill dataset using data adapter
da.Fill(ds, "Patients");
}
catch
{
//don't do anything special, just let .Net handle it
}
finally
{
// close connection and release data adapter
if (conn != null)
{
conn.Close();
da.Dispose();
}
}
//output the dataset
return ds;
}
and here is my codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//create object
Patient ptn = new Patient();
//set datasource of control to objects method
gridview1.DataSource = ptn.GetAllPatients();
//bind
gridview1.DataBind();
}
}
Upvotes: 0
Views: 1259
Reputation: 217
I have found the issue with thanks to my lecturer at SHU.
The problem was that I was creating the dataAdapter:
//create Data adapter
da = new SqlDataAdapter(sql, conn);
But wasnt creating the dataset, like I should here:
//create Data Set
ds = new DataSet("patients");
therefore it was blank
this link was useful too http://support.microsoft.com/kb/314145
Upvotes: 0
Reputation: 11035
Just set:
gridView1.DataMember = "Patients";
You can still return the full set (you might have more tables later, right?) and just let DataMember dictate which to display in-grid.
Upvotes: 1