0

Ok so I have a class for all my database connections, what I want to do is populate my gridview from my select statment within my database class. How do I do this?

Code can be seen below:

Database.cs

  public static string DeleteChild(string Ssession)
        {
            string surname = string.Empty;

            cmd = new SqlCommand("Select Forename,Surname,DOB,Username from Children WHERE fkParentID = (select ParentID from Users Where Username = '" + Ssession + "')", con);
            cmd.Connection = con;
            cmd.Parameters.AddWithValue("@username", Ssession);
            con.Open();
            surname = cmd.ExecuteScalar().ToString();
            con.Close();
            return surname;
        }

GridView.aspx.cs

protected void Details()
        {
            con.Open();

           // string b = Session["LoginSession"].ToString();
            //string forename = DBdatabase.DeleteChild(b);

            SqlCommand cmd = new SqlCommand("Select Forename,Surname,DOB,Username from Children WHERE fkParentID = (select ParentID from Users Where Username = '" + Session["LoginSession"] + "')", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                GridView1.DataSource = ds;
                GridView1.DataBind();
                int columncount = GridView1.Rows[0].Cells.Count;
                GridView1.Rows[0].Cells.Clear();
                GridView1.Rows[0].Cells.Add(new TableCell());
                GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
                GridView1.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
2
Contributors
1
Reply
8
Views
4 Years
Discussion Span
Last Post by LastMitch
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.