Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am creating a web based application (Student Registration Form). In this Web Page I have 1 dropdown list (which shows the lecturetimings like Saturday, Sunday, Monday, Tuesday,........etc). After selecting a list item(ex: Saturday It will show availble courses as Radio buttons (Example:MS-Office,C#.Net). In the selected index change event of drop down list-I have written a switch case: for every different case.
C#
case 0: r1.Visible = false; r2.Visible = false; break; 
case 1: r1.Visible = false; r2.Visible = false; r1.Text = "MS-Office"; r1.Visible = true; break; 
case 2: r1.Visible = false; r2.Visible = false; r1.Text = "Hardware & Networking"; r2.Text="C#.net"; r1.Visible = true; r2.Visible = true; break;

...........
Upto case7 written.

Then I have some text boxes for (StudentName, EmailId, ContactNo.& address).
There is a submit button( on clicking this, the information wil be saved in data base table "registration"(StudentName,EmailId,ContactNo.,Address, Lecturetimings, Course)
Ex:(John, john@john.com, 9999999999, India, Saturday,MS-Office).

Now My requirement is if there 10 records with Saturday then in the dropdown list saturday should not be shown.

I solved this problem with help of a stored procedure.I have taken a data table and I inserted the values of "Lecturetimings" are less than 10 records. When I display the list items from the data table, it is working fine, it is showing only that items which are less than 10 records.

But the problem is it should initially all the list items,If there is no records in the database. And another problem is, when I select any list item then it should show the appropriate radio buttons(Initially it was showing, but after getting the data from the data table it is not working).
Thanks in advance for taking pains for me.

My Tried code so far is :

References: d1=dropdownlist1
	     r1=radiobutton1
	     r2=radiobutton2


Code:


protected void Page_Load(object sender, EventArgs e)
        {
            DataTable records = new DataTable(); 
             string myConnection = "Data Source=server;Initial Catalog=college;User ID=sa;Password=123";
            SqlConnection con = new SqlConnection(myConnection);
            con.Open();
            SqlCommand sqlCommand = new SqlCommand(); 
            sqlCommand.CommandType = CommandType.StoredProcedure; 
            sqlCommand.CommandText = "GetListRecordNotHavingTenCount"; 
            sqlCommand.Connection = con;
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); sqlDataAdapter.Fill(records);
           // d1.DataSource = records;
           // d1.DataValueField = "Id"; 
           // d1.DataTextField = "Lecturetimings"; 
            //this.d1.DataBind();
            
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            
            switch (d1.SelectedIndex)
            {
                case 0:
                    r1.Visible = false;
                    r2.Visible = false;
                    break;
                case 1:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "C Language";
		      r2.Text = "C ++";
		      r1.Visible = true;
r2.Visible = true;
                    break;
                case 2:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Basic Hardware";
                    r1.Visible = true;
                    break;
                case 3:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Networking";
                    r1.Visible = true;
                    break;
                case 4:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Adobe Flash";
			r2.Text = "Adobe Photoshop";

                    r1.Visible = true;
			r2.Visible = true;
                    break;
                case 5:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "C#.Net";
                    r1.Visible = true;
                    break;
                case 6:
                    r1.Visible = false;
                    r2.Visible = false;
                    r1.Text = "Basic Hardware";
                    r1.Visible = true;
                    break;
                case 7:

                    r1.Text = "Adobe Flash";
                    r2.Text = "Networking";
                    r1.Visible = true;
                    r2.Visible = true;
                    break;

            }

        }


        protected void Button1_Click1(object sender, EventArgs e)
        {
            String myConnection = " Data Source=server;Initial Catalog=college;User ID=sa;Password=123";
            SqlConnection con = new SqlConnection(myConnection);
            SqlCommand cmd;
                  
            con.Open();
            cmd = new SqlCommand(@"INSERT INTO [dbo].[Register]([Sname],[Email]
           ,[ContactNo],[Activity],[Lecturetimings])
     VALUES('" + name.Text + "','" + email.Text + "','" + contact.Text + "','" + r1.Text + "','"+d1.SelectedValue+"')", con);
            cmd.ExecuteNonQuery();
            con.Close();
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('SuccessFully Submitted');", true);
            d1.SelectedIndex = 0;
            name.Text = "";
                       email.Text = "";
            contact.Text = "";
            CheckBox1.Checked = false;
            r1.Visible = false;
            r2.Visible = false;
            
                  }
Posted
Updated 17-Jan-13 17:38pm
v4
Comments
Naman Kumar Sinha 16-Jan-13 9:00am    
It would be better if you can post your code over here. Also if you are populating some list control from DB, how can it populate the list when query returns nothing?
2012programmer 16-Jan-13 15:50pm    
References: d1=dropdownlist1
r1=radiobutton1
r2=radiobutton2


Code:


protected void Page_Load(object sender, EventArgs e)
{
DataTable records = new DataTable();
string myConnection = "Data Source=server;Initial Catalog=college;User ID=sa;Password=123";
SqlConnection con = new SqlConnection(myConnection);
con.Open();
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "GetListRecordNotHavingTenCount";
sqlCommand.Connection = con;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand); sqlDataAdapter.Fill(records);
// d1.DataSource = records;
// d1.DataValueField = "Id";
// d1.DataTextField = "Lecturetimings";
//this.d1.DataBind();

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{


switch (d1.SelectedIndex)
{
case 0:
r1.Visible = false;
r2.Visible = false;
break;
case 1:
r1.Visible = false;
r2.Visible = false;
r1.Text = "C Language";
r2.Text = "C ++";
r1.Visible = true;
r2.Visible = true;
break;
case 2:
r1.Visible = false;
r2.Visible = false;
r1.Text = "Basic Hardware";
r1.Visible = true;
break;
case 3:
r1.Visible = false;
r2.Visible = false;
r1.Text = "Networking";
r1.Visible = true;
break;
case 4:
r1.Visible = false;
r2.Visible = false;
r1.Text = "Adobe Flash";
r2.Text = "Adobe Photoshop";

r1.Visible = true;
r2.Visible = true;
break;
case 5:
r1.Visible = false;
r2.Visible = false;
r1.Text = "C#.Net";
r1.Visible = true;
break;
case 6:
r1.Visible = false;
r2.Visible = false;
r1.Text = "Basic Hardware";
r1.Visible = true;
break;
case 7:

r1.Text = "Adobe Flash";
r2.Text = "Networking";
r1.Visible = true;
r2.Visible = true;
break;

}

}


protected void Button1_Click1(object sender, EventArgs e)
{
String myConnection = " Data Source=server;Initial Catalog=college;User ID=sa;Password=123";
SqlConnection con = new SqlConnection(myConnection);
SqlCommand cmd;

con.Open();
cmd = new SqlCommand(@"INSERT INTO [dbo].[Register]([Sname],[Email]
,[ContactNo],[Activity],[Lecturetimings])
VALUES('" + name.Text + "','" + email.Text + "','" + contact.Text + "','" + r1.Text + "','"+d1.SelectedValue+"')", con);
cmd.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('SuccessFully Submitted');", true);
d1.SelectedIndex = 0;
name.Text = "";
email.Text = "";
contact.Text = "";
CheckBox1.Checked = false;
r1.Visible = false;
r2.Visible = false;

}

Thank you Naman Kumar.

1) Rather than having your radio buttons present from the design time, you can dynamically add them.
For eg. You can have LectureTimings table, have Day and Course as columns.
If user selects, Saturday: you should have a query ready to get all the courses in the form of List<Courses>,
iterate that list to generate those many radio buttons dynamically on your screen.
You don't need to write switch case in that case, a for loop will to generate radios will be enough.
2) If there are no items in the list, count will be zero so for loop will not generate any radio button in that case. this resolves your first issue of not having data against a LectureTimings.
Ideally, have a label with Visible=false.
If you have no items in List<Courses>, make label visible and show message that there are no courses for this lecture timing, else let radio radio buttons get generated with the help of loop.
 
Share this answer
 
v4
Comments
2012programmer 16-Jan-13 6:36am    
Thanks "Kishor". As I am new to programming can you let me know with the code to generate radio buttons dynamically.
Kishor Deshpande 16-Jan-13 6:44am    
You can refer link: http://stackoverflow.com/questions/3360755/c-sharp-need-to-dynamically-create-radio-buttons-and-determine-which-value-user
In your case, the code will be like,

public class Course
{
public string CourseName;
public string Day;
}


foreach (Course course in courseList)
{
ii = new RadioButton();
ii.Text = course.CourseName.ToString();
ii.Location = new Point(20, tt);
tt = tt + 20;
panel1.Controls.Add(ii);
}
2012programmer 16-Jan-13 6:46am    
There is one more question, If I create LectureTimings table,then on one-day(ex:Saturday) there are 2 courses(ex:VB,ASP).How can I enter this type of situation in table.
Kishor Deshpande 16-Jan-13 6:49am    
You can create AddCourse(string day, string courseName) method in your data access layer which will add Courses against days in that table.
One more thing, don't make Day or Course as primary key, make both columns composite key.
Thanks.
Kishor Deshpande 16-Jan-13 6:53am    
If you have any query, let me know, if you think this solution helps, please mark this as answer.
Just one or two modifications. From what I understand from your code is that you want to perform two task.
1. Binding data to your dropdownlist.
2. Change the radio button's properties based on you dropdownlist selected index change event.

Before binding data, you can do a small null check like this as shown in the code snippet below.

sqlDataAdapter.Fill(records);
if(records != null)
{
    d1.DataSource = records;
    d1.DataBind();
}


The above code avoids any run time exception if your DataTable or DataSet is null.

Instead of setting separate radio button's behavior. You may consider using RadioButtonList.
Store the subject values in a separate table in your DB and define Foreign Key for relationship with your table where you were executing the stored procedure.

Then you can make dropdownlist.selectedindex as a parameter for the subject search query. This way you avoid a lot of static definitions and require less code change.

Let me know if you need more details on this approach.
 
Share this answer
 
v2
Comments
2012programmer 18-Jan-13 8:20am    
Thank You 'Naman'. I dont have much idea about DB and foriegn keys. Can you give me detail code. One more problem is, Initially if there is no records in my database table- then it should show all the list(Monday,Tuesday,....Sunday).

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