Display the selected values in dropdownlist to Grid View

Neetika
Posted by Neetika under C# category on | Views : 10345
I am Having three dropdown lists named Department,StaffID,StaffType,textbox Name and one submit button...If I select Department as BEMechanical and click the button i have to get all the details regarding that branch and if i select Department,StaffID,stafftype and name i should get the details of the selected one.Any one plz help me..i want this in C# Web Application.

Her is my code which i have tried..

if (DDlDepartment.SelectedValue != "null" && DDLStaffID.SelectedValue != "null")
{
con.Open();
string str = DDlDepartment.SelectedItem.Text;
string str1 = DDLStaffID.SelectedItem.Text;
SqlDataAdapter da = new SqlDataAdapter("select * from ManstaSalary where Department='" + str + "' and StaffID='" + str1 + "';", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
else if (DDlDepartment.SelectedValue != "null")
{


//string str2 = DDlDepartment.SelectedItem.Text;
con.Open();

SqlDataAdapter da1 = new SqlDataAdapter("Select * from ManstaSalary where Department= '" + DDlDepartment.SelectedItem.Text + "'", con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
GridView1.DataSource = ds1;
GridView1.DataBind();


}

Comments or Responses

Posted by: Shankul2784 on: 8/5/2010 Level:Bronze | Status: [Member]

Try this code -

//here i considered as department as primary key(please write those columnname which is not null)

string lsQuery = " select * from ManstaSalary where Department <> '' "


if(DDlDepartment.SelectedValue != "null")
{
lsQuery += " and Department= '" + DDlDepartment.SelectedItem.Text + "'";
}

if(DDLStaffID.SelectedValue != "null")
{
lsQuery += " and StaffID= '" + DDLStaffID.SelectedItem.Text + "'";
}


//write the same code for StaffType & textbox


if(StaffType.SelectedValue != "null")
{
lsQuery += " and StaffType= '" + StaffType.SelectedItem.Text + "'";
}

if(textbox.Text != "null")
{
lsQuery += " and textboxColumnName= '" + textbox.Text + "'";
}



con.Open();
SqlDataAdapter da = new SqlDataAdapter(lsQuery, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();

I hope, this will help you.
Posted by: Neetika on: 8/5/2010 Level:Starter | Status: [Member]
I we assign department as primary key we can't give the department twice..because in one department many students can register.

We are trying to do the following:
1.If we select the department and click the button then that particular department details should be displayed and
2.Again If we select department,staffid,stafftype,name we should get the details of that particular name.

Any suggestions are Welcome..

Posted by: Shankul2784 on: 8/5/2010 Level:Bronze | Status: [Member]

Hi,

If your department will never null then it's ok. You just try that code, it will work.
Posted by: Neetika on: 8/6/2010 Level:Starter | Status: [Member]
I have tried what you have given only second condition is working..

please help me out..
Posted by: Shankul2784 on: 8/9/2010 Level:Bronze | Status: [Member]

Hi,

this means please check this '+=' may be you forget the to ad plus sign.

Posted by: Neetika on: 8/9/2010 Level:Starter | Status: [Member]
I have given all the symbols correctly..

once see my code..


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

public partial class Management_Staff_Salary_Details : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

}
protected void BtnSubmit_Click(object sender, EventArgs e)

{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);

string lsQuery = " select * from ManstaSalary1 where Department <> '' ";


if (DDlDepartment.SelectedValue != "null")

{
lsQuery += " and Department= '" + DDlDepartment.SelectedItem.Text + "'";
}

if (DDLStaffID.SelectedValue != "null")
{
lsQuery += " and StaffID= '" + DDLStaffID.SelectedItem.Text + "'";
}




if (DDlStafftype.SelectedValue != "null")
{
lsQuery += " and StaffType= '" + DDlStafftype.SelectedItem.Text + "'";
}

if (TxtName.Text != "null")
{
lsQuery += " and Name= '" + TxtName.Text + "'";
}



con.Open();

SqlDataAdapter da = new SqlDataAdapter(lsQuery, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

con.Close();
}


}

Any help would be appreciated..
Thanks in advance..



Posted by: Shankul2784 on: 8/9/2010 Level:Bronze | Status: [Member]

Hi,

Just to confirm, is following condition working ?


if (DDlDepartment.SelectedValue != "null")

{
lsQuery += " and Department= '" + DDlDepartment.SelectedItem.Text + "'";
}

And what about the remaining two (3rd & 4th) conditions?
Posted by: Neetika on: 8/10/2010 Level:Starter | Status: [Member]
Hi,

No its not working...
Posted by: Neetika on: 8/10/2010 Level:Starter | Status: [Member]
Hi,
If i execute by keeping only above condition the details of the department are coming..but its not working if i give all the conditions..

If i give all the conditions the details of the particular student is coming...but also i want the details of the particular department if i select only department..Both of the two conditions should work at the same time which i have mentioned in third forum..

Any help would be appreciated..
Thanks in advance..

Posted by: Shankul2784 on: 8/10/2010 Level:Bronze | Status: [Member]

Hi,

If your department query is working fine means code is right. The condition you are using is wrong. In dropdown if you item 'select' & having value 'select' then please change the all if conditions like following

if(StaffType.SelectedValue != "null")

to

if(StaffType.SelectedValue != "select")


Posted by: Neetika on: 8/10/2010 Level:Starter | Status: [Member]
Hi,
IF i give select also its not working only the details of the particular student is coming..


Login to post response