//Check radiobutton list based on the data retrieve from the database

//This is also not working.. This does not select any item of the RadioButtonList1.. and also not giving any error...

    //Here is my code
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectSQL;
            selectSQL = "SELECT * FROM Books_Magazines ";
            selectSQL += "WHERE Ref#='" + DropDownList2.SelectedItem.Text + "'";
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=.;Initial Catalog=ecsd1;User ID=sa;Password=786";
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataReader reader;
            // Try to open database and read information.
            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                reader.Read();
                TextBox1.Text = reader["Book_Name"].ToString();
                TextBox2.Text = reader["Author_Name"].ToString();
                TextBox4.Text = reader["Publisher"].ToString();
                TextBox3.Text = reader["Ref#"].ToString();
                txtEventDate.Text = reader["Date_Of_Purchase"].ToString();
                TextBox6.Text = reader["Vendor_Supplier"].ToString();
                TextBox7.Text = reader["Price"].ToString();



      string result = reader["Type_Of_Purchase"].ToString();// place your retrieved column here
    if(result == "Spot Purchase") 
    {
        RadioButtonList1.Items[0].Selected = true;
    } 
    else if(result == "Tendor Purchase") 
    {
        RadioButtonList1.Items[1].Selected = true;
    }





                reader.Close();

                lblStatus.Text = "";

            }
            catch (Exception err)
            {
                lblStatus.Text = "Error in displaying data. ";
                lblStatus.Text += err.Message;
            }
            finally
            {
                con.Close();
            }


        }




> string result = // place your retrieved column hereif(result == "Spot Purchase) {   RadionButtonList.Items[0].Selected = true;} elseif(result == "Tendor Purchase") {   RadioButtonList.Items[1].Selected = true;}

If the textboxes are being populated with the data then you know the SQL statement is working. In that case, if it is just the checkboxes that aren't being checked the reader must be containing vallues that do not equal "Spot Purchase" or "Tendor Purchase".
Debug your code at the line where result is set to reader["type_of_purchase"] and see what result actually holds.

i set the result of reader["type_of_purchase"] in Labell.Text ,the resultant text of Labell is always "Spot Purchase" or "Tendor Purchase"... but stoll not working...Here is code

Labell.Text= reader["Type_Of_Purchase"].ToString();



         if (Labell.Text == "Spot Purchase")
         {
             RadioButtonList1.SelectedItem.Text = "Spot Purchase";
         }
         else if (Labell.Text == "Tendor Purchase")
         {
             RadioButtonList1.SelectedItem.Text = "Tendor Purchase";
         }

here is another code whisch i tried ... but not working :(

 Labell.Text= reader["Type_Of_Purchase"].ToString();



         if (Labell.Text == "Spot Purchase")
         {
             RadioButtonList1.Items[0].Selected = true;
         }
         else if (Labell.Text == "Tendor Purchase")
         {
             RadioButtonList1.Items[1].Selected = true;
         }

I just tried this myself and I had the same problem until I cleared the radioButtonList before setting one of the items to be selected. Then it worked.

if (Labell.Text == "Spot Purchase")
{
   RadioButtonList1.ClearSelection();
   RadioButtonList1.Items[0].Selected = true;
}
else if (Labell.Text == "Tendor Purchase")
{
   RadioButtonList.ClearSelection();
   RadioButtonList1.Items[1].Selected = true;
}

I'm looking online now to find out why that is the case. If I find something I'll let you know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.