locked
How to display my grid RRS feed

  • Question

  • User-14642827 posted
    protected void btnSearchNames_Click(object sender, EventArgs e)
            {
                string text = "";
                string textboxValue = "";
    
                text = ddlSearchBy.SelectedValue;
                textboxValue = txtSearchByName.Text.ToString();
    
                using (SqlConnection cn = new SqlConnection(Data.DBConnectionString))   
                {
                    //DataTable dt = new DataTable();
                    using(SqlCommand cmd = new SqlCommand("dbo.SearchForStudent", cn))
                    {
                        cn.Open();
                        cmd.CommandType = CommandType.StoredProcedure;
    
                        if ((text != null) && (textboxValue != null) && (text == "LName"))
                        {
                            lastName = text;
                            firstName = " ";
                            ssn = " ";
                            cmd.Parameters.AddWithValue("@lastName", lastName);     // variables declared at the top
                            cmd.Parameters.AddWithValue("@firstName", firstName);   // variables declared at the top
                            cmd.Parameters.AddWithValue("@ssn", ssn);
                            // Read the value using a datareader
                            SqlDataReader dr = cmd.ExecuteReader();
                            DataTable dt = new DataTable();
                            // Fill the datatable using load method
                            ddlResults.DataSource = dt;
                            dt.Load(dr);
                            GVTACStudents.DataBind(); 
                        }
                        else if ((text != null) && (textboxValue != null) && (text == "FName"))
                        {
                            cmd.Parameters.AddWithValue("@firstName", firstName);   // variables declared at the top
                        }
                        else if ((text != null) && (textboxValue != null) && (text == "ssn"))
                        {
                            cmd.Parameters.AddWithValue("@ssn", ssn);               // variables declared at the top
                        }
                        else
                        {
                            return; // alert message "No name or ssn like that.
                        }
                        
                        GVTACStudents.DataBind(); 
                    }
                }      
            }
    
    


    How do I supply the parameter? My I thought I did that.

    Thursday, May 25, 2017 11:29 AM

Answers

  • User-707554951 posted

    Hi bthJ6,

    According to your description, you could bind datasource to gridview.

    You could refer to working code as below:

    protected void btnSearchNames_Click(object sender, EventArgs e)
            {
                string text = "";
                string textboxValue = "";
                text = ddlSearchBy.SelectedValue;
                textboxValue = txtSearchByName.Text.ToString();
                string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection cn = new SqlConnection(connectionString))
                {
                    //DataTable dt = new DataTable();
                    using (SqlCommand cmd = new SqlCommand("SearchForStudent2", cn))
                    {
                        cn.Open();
                        cmd.CommandType = CommandType.StoredProcedure;                   
                        if ((text != null) && (textboxValue != null) && (text == "LName"))
                        {
                            lastName = text;
                            firstName = " ";
                            ssn = " ";
                            cmd.Parameters.AddWithValue("@lastName", lastName);     // variables declared at the top
                            cmd.Parameters.AddWithValue("@firstName", firstName);   // variables declared at the top
                            cmd.Parameters.AddWithValue("@ssn", ssn);
                            // Read the value using a datareader
                            SqlDataReader dr = cmd.ExecuteReader();
                            DataTable dt = new DataTable();
                            // Fill the datatable using load method
                            ddlSearchBy.DataSource = dt;
                            //ddlResults.DataSource = dt;
                            dt.Load(dr);
                            GVTACStudents.DataSource = dt;
                            GVTACStudents.DataBind();
                        }
                        else if ((text != null) && (textboxValue != null) && (text == "FName"))
                        {
                            cmd.Parameters.AddWithValue("@firstName", firstName);   // variables declared at the top
                        }
                        else if ((text != null) && (textboxValue != null) && (text == "ssn"))
                        {
                            cmd.Parameters.AddWithValue("@ssn", ssn);               // variables declared at the top
                        }
                        else
                        {
                            return; // alert message "No name or ssn like that.
                        }
                        GVTACStudents.DataBind();
                    }
                }
            }

    Output Screenshot as follow:

    Best regards,

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 26, 2017 10:08 AM