Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    binding dataset to a grivdview with c#

    Hi, I'm having trouble binding a method to a grid view, could anybody help?. Here is my method:

    Code:
    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:

    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //create object
                Patient ptn = new Patient();
    
                //set datasource of control to objects method
                ddlPtn.DataSource = ptn.GetAllPatients();
    
                //define controls parameters
                ddlPtn.DataTextField = "ptFamilyName";
                ddlPtn.DataValueField = "ptID";
    
                //bind
                ddlPtn.DataBind();
            }
        }
    thanks in advance

  • #2
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, I meant dropdown menu

  • #3
    Regular Coder
    Join Date
    May 2012
    Location
    USA
    Posts
    102
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Are you using Sql Data Source or Object Data Soure to bind the data to a gridview?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •