Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a web application in ASP.NET with C# in which I have 3 DropDownList (ddl) and one DataGridView.
ddl1=CS,IT,A,M
ddl2=FIRST,SECOUND,THIRD
ddl3=FIRST S,SECOUND S

C#
if ddl1.selectedvalue="CS" and ddl2.selectedvalue="FIRST" and ddl3.selectedvalue="FIRST S" then
DataGridView will show the subjects and name of teachers who's teach it for this choice.
and this my code:
C#
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.Visible = false;
    }
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        string cc;
        SqlConnection con = obj.getconn();
        SqlCommand c = new SqlCommand("", con);
        c.CommandText = "selectsubjectname from SUBJECT& Teachername from TEACHER";
        con.Open();
        dr = c.ExecuteReader();
        if ((ddlSearchBy.SelectedIndex == 0)&(ddlSearchBy0.SelectedIndex == 0)&(ddlSearchBy1.SelectedIndex == 0))
        {
            cc = "select subjectname from SUBJECT& Teachername from TEACHER  where SECTION NAME = '"+ ddlSearchBy.SelectedIndex
                 "' And YEAR ='" + ddlSearchBy0.SelectedIndex + "'And SEMESTER='" + ddlSearchBy1.SelectedIndex + "' ";
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "  ** NOT FOUND ** ";
        }
        ds = obj.GetData(cc, "SUBJECT,TEACHER,YEAR,SECTION");
        GridView1.DataSource = ds.row["subjectname from SUBJECT &Teachername from TEACHER"];
        GridView1.DataBind();
        if (GridView1.Rows.Count == 0)
        {
            Label1.Visible = true;
            Label1.Text = "  ** NOT FOUND ** ";
        }
    }



i am stuck , because i dont know where the wrong , plz tell me if this code right or not
Posted
Updated 9-Oct-11 9:56am
v7
Comments
Mehdi Gholam 9-Oct-11 7:05am    
EDIT -> removed bold
Bala Selvanayagam 9-Oct-11 7:07am    
Can you please post your code and tell where you are stuck ?
OriginalGriff 9-Oct-11 7:09am    
Where are you stuck? What is the problem?

Add try.. catch block in the attached code as:
C#
protected void BtnSearch_Click(object sender, EventArgs e)
    {
    try {
      .........
    }
    catch(Exception ex){
      .........
    }


It will point out the exact issue in the code.
 
Share this answer
 
By look at your code, i can see many issues and some of them are as follows. Please correct those issues and see whether you are still getting error messages ? once corrected , please run the code as suggested by "GanesanSenthilvel".

Point 1

Try changing the line [there is no space between select & subjectname in your query ]

c.CommandText = "selectsubjectname from SUBJECT& Teachername from TEACHER";

to
c.CommandText = "select subjectname from SUBJECT, Teachername from TEACHER";


point 2

Change the following [You needs to use comma(,) to retrieve multiple columns not &]

c = "select subjectname from SUBJECT& Teachername from TEACHER  where SECTION NAME = '"+ ddlSearchBy.SelectedIndex

to
cc = "select subjectname from SUBJECT,Teachername from TEACHER  where SECTION NAME = '"+ ddlSearchBy.SelectedIndex


look at your rest of the code and there are places you have used "&" instead of comma for the SQL query column seperator.


point 3

Check the line
ds = obj.GetData(cc, "SUBJECT,TEACHER,YEAR,SECTION");
...not sure what it does


point 4

hope ds is you dataset and in that case, change the followings

GridView1.DataSource = ds.row["subjectname from SUBJECT &Teachername from TEACHER"];


to

GridView1.DataSource = ds.table[0].defaultview();
 
Share this answer
 
v4

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