selecting index change of dropdownlist related prblm - c#

i have a 3 tables: Section,Class and Student...now i want to loading the studentName on the dropdownlist based on selection Class with selecting section in dropdown list[Here Section Field,Class Field will b select by dropdownlist and StudentName also show in to the dropdownlist]
Here is my Table preview
SectionEnty
Id,SectionTitle,Capacity
ClassEntry
Id,ClassTitle,SectionId
StudentInfo
Id,FullName,ClassId,Section
it will b very helpful for me. If any one help me at this situation because i am all ready tried but its not working because of multiple index handling.

ASPX:
<asp:DropDownList ID="classdropdown" runat="server" OnSelectedIndexChanged="classdropdown_SelectedIndexChanged" AutoPostBack="true" />
<asp:DropDownList ID="sectiondropdown" runat="server" OnSelectedIndexChanged="sectiondropdown_SelectedIndexChanged" AutoPostBack="true" />
<asp:DropDownList ID="studentnamedropdown" runat="server" />
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.LoadClassDropdown();
}
}
// set initial values
private void LoadClassDropdown()
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT Id,SectionTitle FROM SectionEnty", "your connection string"))
{
using (DataSet ds = new DataSet())
{
da.SelectCommand.Connection.Open();
da.Fill(ds);
da.SelectCommand.Connection.Close();
classdropdown.DataSource = ds;
classdropdown.DataValueField = "Id";
classdropdown.DataTextField = "SectionTitle";
classdropdown.DataBind();
}
}
}
protected void classdropdown_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT Id,ClassTitle FROM ClassEntry WHERE SectionId = #SectionId", "your connection string")
{
da.SelectCommand.Parameters.Add(new SqlParameter("#SectionId", sectiondropdown.SelectedValue));
using (DataSet ds = new DataSet())
{
da.SelectCommand.Connection.Open();
da.Fill(ds);
da.SelectCommand.Connection.Close();
sectiondropdown.DataSource = ds;
sectiondropdown.DataValueField = "Id";
sectiondropdown.DataTextField = "ClassTitle";
sectiondropdown.DataBind();
}
}
}
protected void sectiondropdown_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT Id,FullName FROM StudentInfo WHERE ClassId = #ClassId", "your connection string"))
{
da.SelectCommand.Parameters.Add(new SqlParameter("#ClassId", classdropdown.SelectedValue));
using (DataSet ds = new DataSet())
{
da.SelectCommand.Connection.Open();
da.Fill(ds);
da.SelectCommand.Connection.Close();
studentnamedropdown.DataSource = ds;
studentnamedropdown.DataValueField = "Id";
studentnamedropdown.DataTextField = "FullName";
studentnamedropdown.DataBind();
}
}
}
Now your studentnamedropdown contains the list for the selected class and section.
Any questions, let me know...

Related

access dataset filled in page_load

I have a Dataset that I fill with two sql tables in the Page_Load event. I fill my DropDownList ddlAirport00 with the values ​​of the first table. But I can not access the filled dataset from the ddlAirport00_SelectedIndexChanged(). It's like the dataset is empty or a Scope of variables trouble.
Someone can help me?
public partial class _Default : System.Web.UI.Page
{
public DataSet ds = new DataSet();
}
my Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
using (SqlConnection scon = new SqlConnection(CS))
{
//this.ds = new DataSet();
sda = new SqlDataAdapter("Select * from Aeropuerto", scon);
sda.Fill(ds, "Aeropuerto");
sda = new SqlDataAdapter("Select * from ALocalidad", scon);
sda.Fill(ds, "Localidad");
sda = new SqlDataAdapter("Select * from Tramo", scon);
sda.Fill(ds, "Tramo");
}
}
And My DDL SelectedIndexChanged() ** I Use Ajax to change the testeeric.Text
protected void ddlAirport00_SelectedIndexChanged(object sender, EventArgs e)
{
if (ds.Tables.Contains("Tramo"))
{
testeeric.Text = DateTime.Now.ToString();
}
else
{
testeeric.Text = "Brasil";
}
}
Since the data retrieving logic is located inside if (!IsPostBack), it is called only at initial page load.
So, you will need to retrieve the data from database again inside SelectedIndexChanged event.
protected void ddlAirport00_SelectedIndexChanged(object sender, EventArgs e)
{
String CS = CnfigurationManager.ConnectionStrings
["MyDatabaseConnectionString1"].ConnectionString;
using (SqlConnection scon = new SqlConnection(CS))
{
sda = new SqlDataAdapter("Select * from Tramo", scon);
sda.Fill(ds, "Tramo");
// Do something.
if (ds.Tables.Contains("Tramo"))
{
testeeric.Text = DateTime.Now.ToString();
}
else
{
testeeric.Text = "Brasil";
}
}
}

Populating a dropdownlist using a SQL Connection

I have a DropdownList in asp.net form that needs populating through SQL. I'm using a ScriptManager in my Page_Load()and due to this dropdownList doesn't get populated. I need to use the ScriptManager since I'm using a AjaxCalendarExtender.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = dba.getNames();
ddNames.DataSource = ds.Tables["EMPLOYEE"].DefaultView;
ddNames.DataTextField = "Username";
ddNames.DataValueField = "Username";
ddNames.DataBind();
}
if (ScriptManager.GetCurrent(Page) == null)
{
Page.Form.Controls.AddAt(0, new ScriptManager());
}
}
getNames() function in DB_Access.cs
public DataSet getNames()
{
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "Select DISTINCT Username from dbo.EMPLOYEE";
SqlDataAdapter da = new SqlDataAdapter(newCmd);
DataSet ds = new DataSet();
da.Fill(ds, "EMPLOYEE");
conn.Close();
return ds;
}
I found the answer to be in the Script Manager in aspx page instead of Creating the it Dynamically in the Page Load event.
<asp:ScriptManager ID="ScriptManager1" runat="server" />

Binding data dynamically in ListView2 on Selected_indexChanged() of ListView1

I am working in a project where i need to list the database child items in listview2 who's parent items reside in listview1 already. here is my listview1 code;
<asp:ListView
ID="ListView1"OnSelectedIndexChanged="ListView1_SelectedIndexChanged" runat="server">
<ItemTemplate>
<a href='<%# Eval("Module_Redirect") %>'> <img src="<%#
Eval("Module_img") %>" /> </a>
</ItemTemplate>
the .cs page code is as follow(which is not working yet!)
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.ListView1.SelectedIndex == 3)
{
SqlDataAdapter da2 = new SqlDataAdapter();
da2.SelectCommand = new SqlCommand("select * from tbl_Forms where Module_ID=3 ", conn);
DataTable dt2= new DataTable();
da2.Fill(dt2);
ListView2.DataSource = dt2;
ListView2.DataBind();
}
}
My point is: how can in fetch the selected item templete of listview1 and show the relevent record in listview2?
you can try this.
using (SqlConnection con = new SqlConnection("YourConnectioString"))
{
using (SqlDataAdapter da2 = new SqlDataAdapter { SelectCommand = new SqlCommand("select * from tbl_Forms where Module_ID = #Module_ID ", con) })
{
// as your using index as the parameter,,
da2.SelectCommand.Parameters.AddWithValue("#Module_ID", ListView1.SelectedIndex);
// or if your trying to pass parameter Module_ID from ListView1 DataKey ,, you can use SelectedDataKey
//da2.SelectCommand.Parameters.AddWithValue("#Module_ID", ListView1.SelectedDataKey);
using (DataTable dt2 = new DataTable())
{
con.Open();
da2.Fill(dt2);
ListView2.DataSource = dt2;
ListView2.DataBind();
}
}
}

How to do a table look up so that my dropdownlist will display the data that I want

I have two tables called facility and Facilitytype_lookup table with relationship with each other using fac_type column field. In the first drop down list I populate the data using fac_type field on facility table, and if the selected fac_type field value is selected, for example selecting LT, the fac_code belonging to that fac_type will be displayed on the second drop down list which is L.337 and L.338 like in the screenshots. The problem is I want the first dropdownlist names to be in full. For etc MR as Meeting Room, DR as discussion Room, LT for leture Theatre. That';s why I created another table called Facilitytype_lookup with the column field called fac_name. How do I make the first drop down list names to be in full names using column field fac_name??
public partial class MainMenu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("select distinct FAC_TYPE from FACILITY", con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds); // fill dataset
ddlFacilityType.DataTextField = ds.Tables[0].Columns["FAC_TYPE"].ToString(); // text field name of table dispalyed in dropdown
// to retrive specific textfield name
ddlFacilityType.DataSource = ds.Tables[0]; //assigning datasource to the dropdownlist
ddlFacilityType.DataBind(); //binding dropdownlist
ddlFacilityType.Items.Insert(0, new ListItem(" Select type", "0"));
}
ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
}
protected void ddlFacilityType_SelectedIndexChanged(object sender, EventArgs e)
{
//string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("select distinct FAC_CODE from FACILITY where FAC_TYPE='" + ddlFacilityType.SelectedValue.ToString() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds); // fill dataset
ddlFacility.DataTextField = ds.Tables[0].Columns["FAC_CODE"].ToString(); // text field name of table dispalyed in dropdown
// to retrive specific textfield name
ddlFacility.DataSource = ds.Tables[0]; //assigning datasource to the dropdownlist
ddlFacility.DataBind(); //binding dropdownlist
ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
}
protected void ddlFacility_SelectedIndexChanged(object sender, EventArgs e)
{
Session["roomvalue"] = ddlFacility.SelectedValue;
if (ddlFacilityType.Text == "MR")
{
Response.Redirect("number1.aspx");
}
else if (ddlFacilityType.Text == "DR")
{
Response.Redirect("number1.aspx");
}
else
Response.Redirect("number2.aspx");
}
}
Edited, first dropdownlist will display fac_name values, but after choosing any option in first drop down list, second drop down list doesn't come out anything
public partial class MainMenu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("select distinct facility.FAC_TYPE, facilitytype_lookup.fac_name from FACILITY inner join facilitytype_lookup ON facility.fac_type=facilitytype_lookup.fac_type", con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds); // fill dataset
ddlFacilityType.DataTextField = ds.Tables[0].Columns["FAC_name"].ToString(); // text field name of table dispalyed in dropdown
// to retrive specific textfield name
ddlFacilityType.DataSource = ds.Tables[0]; //assigning datasource to the dropdownlist
ddlFacilityType.DataBind(); //binding dropdownlist
ddlFacilityType.Items.Insert(0, new ListItem(" Select type", "0"));
}
ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
}
protected void ddlFacilityType_SelectedIndexChanged(object sender, EventArgs e)
{
//string constr = ConfigurationManager.ConnectionStrings["serverConnectionString"].ToString(); // connection string
string constr = ConfigurationManager.ConnectionStrings["projectConnectionString"].ToString();
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("select distinct FAC_CODE from FACILITY where FAC_TYPE='" + ddlFacilityType.SelectedValue.ToString() + "'", con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds); // fill dataset
ddlFacility.DataTextField = ds.Tables[0].Columns["FAC_CODE"].ToString(); // text field name of table dispalyed in dropdown
// to retrive specific textfield name
ddlFacility.DataSource = ds.Tables[0]; //assigning datasource to the dropdownlist
ddlFacility.DataBind(); //binding dropdownlist
ddlFacility.Items.Insert(0, new ListItem(" Select room", "0"));
}
protected void ddlFacility_SelectedIndexChanged(object sender, EventArgs e)
{
Session["roomvalue"] = ddlFacility.SelectedValue;
if (ddlFacilityType.Text == "MR")
{
Response.Redirect("number1.aspx");
}
else if (ddlFacilityType.Text == "CR")
{
Response.Redirect("number1.aspx");
}
else if (ddlFacilityType.Text == "DR")
{
Response.Redirect("number1.aspx");
}
else
Response.Redirect("number2.aspx");
}
}

Gridview display

I dont know what it is but i'm having all sorts of issues with this gridview. Below is the code but the issues is the grid is not displaying. Visibility is set to true and the query does return results. So I'm asking for another set of eyes to point out what went wrong here.
Thank you
protected void btnDisplay_Click(object sender, EventArgs e)
{
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\levels.mdb";
DataSet ds;
using (OleDbConnection myConnString = new OleDbConnection())
{
myConnString.ConnectionString = connString;
using (OleDbCommand selectCommand = new OleDbCommand())
{
selectCommand.CommandText = "select * from tblTest";
selectCommand.Connection = myConnString;
myConnString.Open();
using(OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = selectCommand;
ds = new DataSet();
da.Fill(ds, "test");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}//end click event
and the gridview
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
or
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
data source should have been:
GridView1.DataSource = ds.Tables["test"];
GridView1.DataBind();

Categories