I have a problem when retrieve the value of my radio button list from database for editing purpose it will retrieve all of the inserted values and select the right one but it will give more than 20 radio button in the form
this is my code :
SqlDataAdapter da = new SqlDataAdapter("SELECT dbo.tblType.Type_ID, dbo.tblType.Type_Name FROM dbo.tblReg_Details INNER JOIN dbo.tblType ON dbo.tblReg_Details.Type_ID = dbo.tblType.Type_ID",con);
DataSet ds_type = new DataSet();
da.Fill(ds_type, "tblType");
rdblType.DataTextField = "Type_Name";
rdblType.DataValueField = "Type_ID";
rdblType.DataSource = ds_type.Tables["tblType"];
rdblType.DataBind();
foreach (ListItem litem2 in rdblType.Items)
{
if (litem2.Text == DS.Tables[0].Rows[0].ItemArray[6].ToString())
{
litem2.Selected = true;
}
}
kindly help me how to fix this error
Parveen SiwachPosted May 21, 2013, 3:51 AM
string myconn = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
private void BindRadioButton()
{
using (SqlConnection con = new SqlConnection(myconn))
{
string sqlstr = "Select dbo.tblType.Type_ID, dbo.tblType.Type_Name from dbo.tblType";
con.Open();
SqlCommand cmd = new SqlCommand(sqlstr, con);
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 15;
SqlDataAdapter objDataAdapter = new SqlDataAdapter(cmd);
DataSet dsType = new System.Data.DataSet(cmd.CommandText);
objDataAdapter.Fill(dsType, "tblType");
rdblType.DataTextField = "Type_Name";
rdblType.DataValueField = "Type_ID";
rdblType.DataSource = dsType.Tables["tblType"];
rdblType.DataBind();
}
}
private void GetSelectedValue()
{
using (SqlConnection con = new SqlConnection(myconn))
{
int regId = 7;
string sqlstr = string.Format("SELECT dbo.tblType.Type_ID, dbo.tblType.Type_Name FROM dbo.tblReg_Details INNER JOIN dbo.tblType ON dbo.tblReg_Details.Type_ID = dbo.tblType.Type_ID AND ID={0}", regId);
con.Open();
SqlCommand cmd = new SqlCommand(sqlstr, con);
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 15;
SqlDataReader mySqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleRow);
while (mySqlDataReader.Read())
{
foreach (ListItem litem2 in rdblType.Items)
{
if (litem2.Text == Convert.ToString(mySqlDataReader["Type_Name"]))
{
litem2.Selected = true;
break;
}
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
BindRadioButton();
GetSelectedValue();
}
This is working as per your requirement.
ToBePosted May 22, 2013, 3:43 AM
Parveen SiwachPosted May 21, 2013, 7:50 AM
If you need to get data based on Ser_no just change the query as:
string serialNo= "0001-a";
string sqlstr = string.Format("SELECT dbo.tblType.Type_ID, dbo.tblType.Type_Name FROM dbo.tblReg_Details INNER JOIN dbo.tblType ON dbo.tblReg_Details.Type_ID = dbo.tblType.Type_ID AND Ser_no={0}", serialNo);
Try this.
If this doesn't solve your issue, you can reach me on my no.: 9990678013 :)
ToBePosted May 21, 2013, 7:32 AM
why did u used fixed reg Id
i have the serial no as string so when i run the code it will take the first digit from the serial no and will give error invalid column name :(
ToBePosted May 21, 2013, 2:38 AM
attached files is contain one form and the DB backup
thanks again .
Parveen SiwachPosted May 20, 2013, 1:53 AM
ToBePosted May 19, 2013, 6:51 AM
I tried what you suggest but still it's not working
would you kindly show it to me exactly to my code above .
Parveen SiwachPosted May 17, 2013, 7:14 AM
As per your details, to bind radio button list, get data from "tblType" table:
Select
dbo.tblType.Type_ID,
dbo.tblType.Type_Name
from
dbo.tblType
And to select the radio button with selected value:
Select Type_Id from tblReg_Details Where Id=
This will solve your problem.
ToBePosted May 15, 2013, 4:24 AM
thank you for your reply see my main Reg_details table is having an ID of the type if it's Art the Id is 0 and so on so the ID will be saved in the main table.
like this
This is my tblReg_Details
ID
Ser_no
Type_ID
Address
tel
SR_ID
1
0001-a
1
ewrwtrett
1213212342543
A335
2
0002-a
0
Fdgdfgrty23423
454234324
A336
3
0003-a
2
Rgtrgfcg4344
36572813678
A337
And this is my tblType Table :
Which has fixed value
Type_ID
Type_name
0
Art
1
History
2
Literature
3
Theater
so if the user selected before History it should retrieve the 4 radiobuttons only
ART History Literature Theater
with only history Selected .
I don't want to add more Primary key is it possible or not .
Parveen SiwachPosted May 15, 2013, 3:56 AM
As per my understanding by mistake you have used "cartesian join", thats why it is giving multiple values.
To solve the problem use the following query:
To bind radio button list (From master table i.e. tblType)
Select
dbo.tblType.Type_ID,
dbo.tblType.Type_Name
from
dbo.tblType
where
dbo.tblType.Type_Id=1
Note: tblType table must have one primary key column lets suppose "Id" and other two were "Type_Id" and "Type_Name".
From detail table you only need the selected data that can be retrieved easily like:
Select Type_Id from tblReg_Details Where Reg_Id=5
Note: In tblReg_Details table coulmn "Type_Id" is the foreign key of "Id" of "tblType" table.
Hope this will help you to solve your problem.