fergus

fergus

  • 1.4k
  • 232
  • 1.1k

cant use value of textbox to select data from sql table

Apr 12 2024 11:58 AM

I am trying to run a select statement to pull in data from my sql table. i am trying to use TextBox1.Text to run the select for the gridview but the grid doesnt show. when i hardcode the values into the WHERE clause it works so i know the data being pulled in is valid. here is the code i am working with

 

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridView();
    }

    if (Session["userId"] == null)
        Response.Redirect("Login.aspx");
    SessionLabel.Text = "Username : " + Session["userId"];

    SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings["croskConn"].ToString());

    try
    {
        string uid = Convert.ToString(Session["userId"]);

        myCon.Open();
        string qry = "select userId, FirstName, Surname, Email, ReferenceNum from users where userId='" + uid + "'";
        SqlCommand cmd = new SqlCommand(qry, myCon);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adapter.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            string firstName = dt.Rows[0]["FirstName"].ToString();
            lblFirstName.Text = firstName;
            string surName = dt.Rows[0]["Surname"].ToString();
            lblSurName.Text = surName;
            string email = dt.Rows[0]["Email"].ToString();
            lblEmail.Text = email;
            string refNum = dt.Rows[0]["ReferenceNum"].ToString();
            lblRefNum.Text = refNum;
            TextBox1.Text = refNum;
        }
        else
        {
            lblMsg.Text = "No record found!";
        }
        myCon.Close();

    }
    catch (Exception ex)
    {
        lblMsg.Text = ex.Message;
        Response.Write(ex.Message);
    }

}




protected void ButtonLogout_Click(object sender, EventArgs e)
{
    Session.Abandon();
    Response.Redirect("Login.aspx");

}

private void BindGridView()
{
    string userId = Session["userId"].ToString();
    string fergus = refNum.text;
      

    string connectionString = ConfigurationManager.ConnectionStrings["croskConn"].ConnectionString;
    string query = "SELECT client_ref, crosk_ref, client_name, section_name, curr_status FROM md_overall_temp  where client_Code ='" + TextBox1.Text + "'";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(query, connection);
        command.Parameters.AddWithValue("@userId", userId);

        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            GridViewMatters.DataSource = dt;
            GridViewMatters.DataBind();
        }
        else
    {
        GridViewMatters.DataSource = null;
        GridViewMatters.DataBind();
        NoRecordsExist.Visible = true; // Show the label when no records exist
    }

    }
}

 


Answers (1)