using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class exam : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    public void function()
        my e = new my();
        SqlConnection sqlcon = new SqlConnection(e.ImportantData);
        SqlCommand sqlcmd;
        sqlcmd = new SqlCommand("usp_exam1", sqlcon);
        sqlcmd.Parameters.AddWithValue("@rowno", '1');
        sqlcmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(sqlcmd);
        DataSet ds = new DataSet();
        RadioButton[][] radioButtons = new RadioButton[3][];
        TableRow[] tr = new TableRow[3];
        TableCell[][] tc = new TableCell[3][];
        if (ds.Tables[0].Rows.Count > 0)

            int j=0;
            int i = 0;
            Table tb = new Table();

                    for(int k=1;k<5;k++)
                    DataRow dtr = ds.Tables[0].Rows[j];
                    tr[j] = new TableRow();
                    tc[i][j] = new TableCell();
                    [COLOR="Red"]radioButtons[i][j] = new RadioButton();[/COLOR]
                    radioButtons[i][j].Text = dtr["Option"+"k"].ToString();


            Response.Write("wrong user id and password");


    protected void Button2_Click(object sender, EventArgs e)

I am creatig dymaic 12 radio button in the series of 4(option) but I am getting error(red line).
as I am very new to asp.net I am not able to handle it

Edited by diafol: fixed formatting

7 Years
Discussion Span
Last Post by nick.crane

I have faced same problem once and in my case the form.findControl was also useless.

So what I did was create a datatable and bind it to gridview, and on each (add) click, insert a new row in the datatable and rebind the grid, and when you want to get values of the controls, just use i.e.

Also names of your radiobuttons must be in a sequence like rbtn1, rbtn2

int count = 0;
foreach (gridViewRow grRbtn in gridView1.Rows)
   radiobutton rbtn = grRbtn.findControl("rbtn"+count) as radiobutton;
   // do what you have to do, youve got the object of radio button
   // i.e. store them in an array.
   count ++;

I think that your problem lies here.

RadioButton[][] radioButtons = new RadioButton[3][];
TableRow[] tr = new TableRow[3];
TableCell[][] tc = new TableCell[3][];

In your table creation loops you are creating 4 rows with 4 columns and adding 4 radiobutton to each cell. Yet you only have three rows in the arrays and no columns.

Also, I think that this DataRow dtr = ds.Tables[0].Rows[j]; would be better outside the k for loop.

Edited by nick.crane: n/a


Hello nick,
Thanks for the reply

But when I use the braekpoint the cursor is not going on to that line where I add the radio button to cells
the error is coming to that line so I dont think so the error is related to how many radio button is being added to cell.
any how change the row from 4 to 3 still getting the same error

Please see those two lines

RadioButton[][] radioButtons = new RadioButton[3][];
radioButtons[j] = new RadioButton();

these declaration may contain the error


Yes, that is what I was pointing out. You only initialise the primary array part here. RadioButton[][] radioButtons = new RadioButton[3][]; So when you try to execute radioButtons[i][j] = new RadioButton(); radioButtons[j] does not exist because radionButtons is null.

Put a breakpoint after RadioButton[][] radioButtons = new RadioButton[3][]; and examine the contents of radioButtons.

Edited by nick.crane: n/a


Ya , Its seems that your right, plz tell me what should I do to overcome this problem

means what change should be done in code


Option 1) Change the array to a two dimensional array.

RadioButton[,] radioButtons = new RadioButton[4, 4];

Option 2) initialise the second tier of arrays.

RadioButton[][] radioButtons = new RadioButton[4][];
for (int i = 0; i < radioButtons.Length; i++)
    radioButtons[i] = new RadioButton[4];

Option 3) If you don't know how many you need then use a list.

List<RadioButton> radioButtons = new List<RadioButton>();
RadioButton newRB = new RadioButton();

Edited by nick.crane: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.