locked
why data not showing RRS feed

  • Question

  • User-807418713 posted

    hello

    this is my code not working

            SqlConnection cony = new SqlConnection(ConfigurationManager.ConnectionStrings["invConnectionString"].ConnectionString);
            cony.Open();
            SqlCommand cmd1 = new SqlCommand("select Replace(CONVERT(VARCHAR(15), DateOfIssue, 106), ' ', '-') as Old_DateOfIssue from reg where BatchNo='" + DropDownList1.Text + "'", cony);
            SqlDataReader sdr1 = cmd1.ExecuteReader();
            while (sdr1.Read())
            {
                Label7.Text = sdr1["Old_DateOfIssue"].ToString();
            }
    
            cony.Close(); 
    

    if i pass it below code it showing perfect but above code not working why

            SqlConnection cony = new SqlConnection(ConfigurationManager.ConnectionStrings["invConnectionString"].ConnectionString);
            cony.Open();
            SqlCommand cmd1 = new SqlCommand("select Replace(CONVERT(VARCHAR(15), DateOfIssue, 106), ' ', '-') as Old_DateOfIssue from reg where BatchNo='A-123'", cony);
            SqlDataReader sdr1 = cmd1.ExecuteReader();
            while (sdr1.Read())
            {
                Label7.Text = sdr1["Old_DateOfIssue"].ToString();
            }
    
            cony.Close(); 
    

    whats the issues

    Thursday, June 4, 2020 5:21 AM

Answers

  • User197322208 posted
    1. this is my code not working
      That means generating error  ? Did you debug ? The
      (sdr1.Read()
      return false? 
    2. Please read something about SqlInjection 
      where BatchNo='" + DropDownList1.Text + "'"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2020 6:00 AM
  • User288213138 posted

    Hi Gopi.MCA,

    SqlCommand cmd1 = new SqlCommand("select Replace(CONVERT(VARCHAR(15), DateOfIssue, 106), ' ', '-') as Old_DateOfIssue from reg where BatchNo='" + DropDownList1.Text + "'", cony);

     SqlCommand cmd1 = new SqlCommand("select Replace(CONVERT(VARCHAR(15), DateOfIssue, 106), ' ', '-') as Old_DateOfIssue from reg where BatchNo='A-123'", cony);

    The difference between these 2 pieces of code is the value of BatchNo, they are correct in grammar.

    so please show me your error message and make sure there is a value in the Dropdownlist1.text when executing the query.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2020 6:34 AM

All replies

  • User197322208 posted
    1. this is my code not working
      That means generating error  ? Did you debug ? The
      (sdr1.Read()
      return false? 
    2. Please read something about SqlInjection 
      where BatchNo='" + DropDownList1.Text + "'"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2020 6:00 AM
  • User-2054057000 posted

    The best way to debug is to put a breakpoint in visual studio in the below line:

    Label7.Text = sdr1["Old_DateOfIssue"].ToString();

    This will let you get the error description if any. You can also refer to article on Read Records with ADO.NET

    Thursday, June 4, 2020 6:18 AM
  • User288213138 posted

    Hi Gopi.MCA,

    SqlCommand cmd1 = new SqlCommand("select Replace(CONVERT(VARCHAR(15), DateOfIssue, 106), ' ', '-') as Old_DateOfIssue from reg where BatchNo='" + DropDownList1.Text + "'", cony);

     SqlCommand cmd1 = new SqlCommand("select Replace(CONVERT(VARCHAR(15), DateOfIssue, 106), ' ', '-') as Old_DateOfIssue from reg where BatchNo='A-123'", cony);

    The difference between these 2 pieces of code is the value of BatchNo, they are correct in grammar.

    so please show me your error message and make sure there is a value in the Dropdownlist1.text when executing the query.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2020 6:34 AM
  • User-807418713 posted

    Hello

    I used this code 

    SqlCommand cmd1 = new SqlCommand("select Replace(CONVERT(VARCHAR(15), DateOfIssue, 106), ' ', '-') as Old_DateOfIssue from reg where BatchNo='"+DropDownList1.Text+"'", cony);

    data showing plain whats the reason if i pass dropdownlist1 its not showing

    Thursday, June 4, 2020 6:58 AM
  • User288213138 posted

    Hi Gopi.MCA,

    data showing plain whats the reason if i pass dropdownlist1 its not showing

    If there is a value in DropDownList1.Text, then you also need to ensure that DropDownList1.Text is equal to the value in the BatchNo field.

    If you still can't solve the problem, please post the complete code.

    Best regards,

    Sam

    Thursday, June 4, 2020 7:08 AM
  • User-807418713 posted

    Hello

    Is it possible to get that selected item of dropdwonlist data in some hiddenfield and pass in where clasue to check if that works?

    how  would be the code

    Thanks

    Thursday, June 4, 2020 9:00 AM
  • User288213138 posted

    Hi Gopi.MAC,

    Is it possible to get that selected item of dropdwonlist data in some hiddenfield and pass in where clasue to check if that works?

    I don't understand what your mean, can you explain in detail?

    Best regards,

    Sam

    Thursday, June 4, 2020 9:11 AM