Click here to Skip to main content
16,020,253 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

How to retrieve value of chkbox from radio button selected index ??

CODE IS :

XML
<asp:RadioButtonList ID="rdoSr" runat="server" AutoPostBack="True"
                          onselectedindexchanged="rdoSr_SelectedIndexChanged">
          </asp:RadioButtonList>




XML
<asp:CheckBoxList ID="chkSr" runat="server">
          </asp:CheckBoxList>



DATABASE

SQL
CREATE TABLE [dbo].[Customer](
    [Sr_No] [int] IDENTITY(1,1) NOT NULL,
    [City] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [phone] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [Population] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
    [Sr_No] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]




SQL
CREATE TABLE [dbo].[State](
    [Sr_No] [int] IDENTITY(1,1) NOT NULL,
    [City] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [State] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
 CONSTRAINT [PK_State] PRIMARY KEY CLUSTERED
(
    [Sr_No] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]




SQL
ALTER procedure [dbo].[Customer_FullJoin_State_Retrieve]
@SNo int
as
select Customer.City AS City1,State.City AS City2,Customer.Sr_No as Sr_No1, * from Customer full Join State on Customer.City = State.City
where Customer.Sr_No=@SNo
RETURN



Main Code

C#
protected void rdoSr_SelectedIndexChanged(object sender, EventArgs e)
   {

       SqlConnection conn = new SqlConnection(ConnString);
       SqlCommand cmd = new SqlCommand("Customer_FullJoin_State_Retrieve", conn);
       cmd.CommandType = CommandType.StoredProcedure;
       cmd.Parameters.Add("@SNo", rdoSr.SelectedItem.Value);

       LoadRetreive3();// bind value of rdoSr from Customer table
       LoadRetreive4();//bind value of chkSr from State table

       conn.Open();
       SqlDataReader reader = cmd.ExecuteReader();
       reader.Read();
       {
           chkSr.SelectedItem.Text=reader["State"].ToString();  // here's the error


           reader.Close();
       }
       conn.Close();
   }


I want chkSr value(selected one) from database and table name "State"... rdoSr contains same values of state column and I want after select on rdoSr, the value of chksr comes up automatically. Both rdoSr and chksr contains same values dat's retrieve from column Sate of table State
Posted
Updated 11-May-11 7:55am
v3

If I understand your query correctly you're trying to change the label of the checkbox (i.e. the text that appears next to it) to text you're loading from a database.

Looks like you're successfully getting the text from the database. However, you need to execute
chkSr.Text=reader["State"].ToString();

and not
chkSr.SelectedItem.Text=reader["State"].ToString();


In other words, assign the text you loaded from the database to the Text property of the chkSr checkbox directly; remove the "SelectedItem" portion.
 
Share this answer
 
Comments
sat_100m 11-May-11 21:55pm    
No I am trying to get check value of checkbox chksr from database....
Wonde Tadesse 12-May-11 7:29am    
State column is not available in the SP.
R. Hoffmann 12-May-11 7:58am    
Isn't it? Doesn't the asterisk in the SELECT include all fields from the State table as well?

<pre>
select Customer.City AS City1,State.City AS City2,Customer.Sr_No as Sr_No1, * from Customer full Join State
</pre>
Wonde Tadesse 12-May-11 9:40am    
My friend Full Join means bring any data from right and left tables. Not select * columns.
Please read http://www.w3schools.com/sql/sql_join_full.asp
R. Hoffmann 12-May-11 9:47am    
I just created a new empty database in SQL Server 2008, and ran the schema creation scripts that OP provided. Then I ran the stored proc, and it does return the State field.
Your StoredProcedure returns City1,City2,Sr_No1 and All columns from Customer table. But your are trying to access a Table name called State using DataReader read method. Please see the how to use Retrieving Data Using a DataReader (ADO.NET)http://msdn.microsoft.com/en-us/library/haa3afyz.aspx[^]

Another option,

Improve your SP to include State Column

SQL
ALTER procedure [dbo].[Customer_FullJoin_State_Retrieve]
@SNo int
as
select Customer.City AS City1,State.City AS City2, State.State AS State, Customer.Sr_No as Sr_No1, * from Customer full Join State on Customer.City = State.City
where Customer.Sr_No=@SNo
RETURN


I hope this will helps you well.
 
Share this answer
 
v4
Comments
R. Hoffmann 12-May-11 8:01am    
Wouldn't the fact that the original query contains an asterisk mean that all fields from the joined tables are returned, even if not specifically listed by name?
Wonde Tadesse 12-May-11 9:39am    
My friend Full Join means bring any data from right and left tables. Not select * columns.
Please read http://www.w3schools.com/sql/sql_join_full.asp
R. Hoffmann 12-May-11 9:48am    
See my reply above :-)
try this

take that {reader["State"].ToString() value and compare it with l.text } value in string
and then

foreach (ListItem l in CheckBoxList1.Items)
            {
                if (l.Text == "Your Data Base Value")
                {
                    l.Selected = true;
                }
            }
 
Share this answer
 
Comments
Wonde Tadesse 12-May-11 7:28am    
State column is not available in the SP.
sat_100m 12-May-11 9:15am    
Yes I did little bit but big changes in DB .

ALTER procedure [dbo].[quest_categories_FullJoin_State_Retrieve]
@SNo int
as
select quest_categories.City AS City1,State.City AS City2,quest_categories.Sr_No as Sr_N01,
phone,Population,State.City AS City2,State
from quest_categories
full Join State on quest_categories.City = State.City

where quest_categories.Sr_No=@SNo
RETURN
sat_100m 12-May-11 9:11am    
Thnx Mahendra.p25....u r correct..........

Thank You very Much
Mahendra.p25 13-May-11 0:30am    
Welcome
Okay, since I misunderstood your question the first time around...

The first thing to note is that you need to set the chkSr.Checked property, not the Text property, to check/uncheck the control. This is a boolean property, meaning it only accepts true and false values.

Secondly, you'll need to have a way to convert the string value you're reading from the database to a boolean value that you can use to check/uncheck the checkbox. The way to do this will depend on your requirements, i.e. when you want the checkbox to be checked.

For example, lets assume you want the checkbox to be ticked when the state is "MyState". Then you'd do something like this:

if (reader["State"].ToString() == "MyState")
    chkSr.Checked = true;
else
    chkSr.Checked = false;


Hope this helps.
 
Share this answer
 

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