Click here to Skip to main content
16,016,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to fetch data from database in a radio button,for example i have married and unmarried two radio buttons....

Code;

C#
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);
 SqlCommand com = new SqlCommand("Select First_Name , Middle_Name , Last_Name , Mobile_No , Email, DOB , Age , Education , Office_Add , Native_Add , PAN_Card , Aadhar_Card , Religion , Business_Job , Married_Unmarried , No_Of_Members from Primary_Member where Flat = '" + Request.QueryString["fnum"] + "'", conn);
 conn.Open();
 SqlDataReader dr = com.ExecuteReader();
 if (dr.Read())
 {
 TextBoxFN.Text = dr["First_Name"].ToString();
 TextBoxMN.Text = dr["Middle_Name"].ToString();
 TextBoxLN.Text = dr["Last_Name"].ToString();
 TextBoxMO.Text = dr["Mobile_No"].ToString();
 TextBoxE.Text = dr["Email"].ToString();
 TextBoxDOB.Text = dr["DOB"].ToString();
 TextBoxA.Text = dr["Age"].ToString();
 TextBoxEQ.Text = dr["Education"].ToString();
 TextBoxOA.Text = dr["Office_Add"].ToString();
 TextBoxNA.Text = dr["Native_Add"].ToString();
 TextBoxPCN.Text = dr["PAN_Card"].ToString();
 TextBoxACN.Text = dr["Aadhar_Card"].ToString();
 TextBoxR.Text = dr["Religion"].ToString();
 TextBoxNOM.Text = dr["No_Of_Members"].ToString();

 dr.Close();
 conn.Close();
 }
Posted
Updated 14-Oct-15 0:31am
v2
Comments
Richard Deeming 19-Oct-15 12:42pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Assume that your data in datatable and based on condition we can select the radio button like below.

C#
string strGender=dt.Rows[0]["Gender"].ToString();
if(strGender=="Male")
   rbdGender.SelectedIndex=0;
else
   rbdGender.SelectedIndex=1;
 
Share this answer
 
Comments
Member 12049271 14-Oct-15 7:02am    
Naveen Sanagasetti...
I don't have my data in data table instead i have used sqldatareader to fetch data from database...So can you please give me code for that.
Once you have the datatable, you can go something like this,
rdbMarried.SelectedIndex = Convert.ToInt32(rdbMarried.Items.IndexOf(rdbMarried.Items.FindByText(dt.Rows[0]["Married"].ToString())));
// as you haven't shared your code, I did it with my assumption
// dt is a datatable
// Married is a column name


-KR
 
Share this answer
 
Comments
Member 12049271 14-Oct-15 2:48am    
I'm not using DataTable instead i have used SqlReader to read from my database

My Code is as follows...

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HousingConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand("Select First_Name , Middle_Name , Last_Name , Mobile_No , Email, DOB , Age , Education , Office_Add , Native_Add , PAN_Card , Aadhar_Card , Religion , Business_Job , Married_Unmarried , No_Of_Members from Primary_Member where Flat = '" + Request.QueryString["fnum"] + "'", conn);
conn.Open();
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
TextBoxFN.Text = dr["First_Name"].ToString();
TextBoxMN.Text = dr["Middle_Name"].ToString();
TextBoxLN.Text = dr["Last_Name"].ToString();
TextBoxMO.Text = dr["Mobile_No"].ToString();
TextBoxE.Text = dr["Email"].ToString();
TextBoxDOB.Text = dr["DOB"].ToString();
TextBoxA.Text = dr["Age"].ToString();
TextBoxEQ.Text = dr["Education"].ToString();
TextBoxOA.Text = dr["Office_Add"].ToString();
TextBoxNA.Text = dr["Native_Add"].ToString();
TextBoxPCN.Text = dr["PAN_Card"].ToString();
TextBoxACN.Text = dr["Aadhar_Card"].ToString();
TextBoxR.Text = dr["Religion"].ToString();
TextBoxNOM.Text = dr["No_Of_Members"].ToString();

dr.Close();
conn.Close();
}

I have two Radio Buttons one for Married and another for unmarried...
Krunal Rohit 14-Oct-15 6:42am    
rdbMarried.SelectedIndex = Convert.ToInt32(rdbMarried.Items.IndexOf(rdbMarried.Items.FindByText(dr["Married"].ToString())));

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900