none
checkbox and conditions RRS feed

  • Question

  • hi

    i applied condition in a program with checkbox , it works fine when i check the checkbox1 and press button it display all the data , problems come when i check the "checkbox2" it dont show me data just show the colums . i h ave entered the right values but same result.

    the value of checkbox is stored in checkbox1 and checkbox2 respectively by doing this checkox1 = checkBox1.Checked.ToString(); similarly for checkbox2. Forms are inherited too .

    if (checkbox1 == true.ToString())
                    {
                        SqlConnection sqlconnection = new SqlConnection();
                        sqlconnection.ConnectionString = _con;
                        sqlconnection.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = sqlconnection;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = "SELECT UserName ,LoginName ,Date ,Time,IP FROM UserInfo where LoginName = '" + textbox1.ToString().Trim() + "'";
                        DataSet ds7 = new DataSet();
                        SqlDataAdapter adp7 = new SqlDataAdapter(cmd);
                        adp7.Fill(ds7, "UserIn");
                        dataGridView1.DataSource = ds7;
                        dataGridView1.DataMember = "UserIn";
                        sqlconnection.Close();
                    }
                    else

    if (checkbox2 == true.ToString())
                    {
                        SqlConnection sqlconnection = new SqlConnection();
                        sqlconnection.ConnectionString = _con;
                        sqlconnection.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = sqlconnection;
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandText = "SELECT UserName ,LoginName ,Date ,Time,IP FROM UserInfo where UserName = '" + textbox2.ToString().Trim() + "'";
                        DataSet ds8 = new DataSet();
                        SqlDataAdapter adp8 = new SqlDataAdapter(cmd);
                        adp8.Fill(ds8, "UserIn");
                        dataGridView1.DataSource = ds8;
                        dataGridView1.DataMember = "UserIn";
                        sqlconnection.Close();
                    }

     

    Sunday, March 28, 2010 12:21 AM

Answers

All replies

  • Hi,

     

    Seems there is no error in your code.

     

    I suggest you to check the command text again. You can use:

    Console.WriteLine(cmd.CommandText);

    And copy the command text, run it in SQL server. The result should be the same as data display in datagridview.

     

    Look forward to your good news. :)

     

    Best regards,

    Ling Wang

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, April 1, 2010 9:48 AM
    Moderator
  • :) solved wrong value of textbox was different
    Saturday, April 3, 2010 7:48 AM
  • I’m glad to hear that you got it working. Thank you for sharing your experience here.
    It will be very beneficial for other community members having the similar questions.

    Have a nice day! :)


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, April 7, 2010 2:26 AM
    Moderator