Cascading for multiple selection of checkbox list - asp.net

I want to retrieve data from oracle data base for one Checkbox list to another checkbox List for multiple selection in asp.net.
But unfortunately i am getting same ID again again while debugging.
Please help me Where i am doing mistake.
Is there any another easy approach on same.
I want something like : "Select d.depot_code, d.depot_description from table where d.depot in (depot_code from another Checkbox List) " [with comma separated ID]
CODE:
ddlregion Binding code:
public void BindRegion()
{
OracleCommand Cmd = new OracleCommand("select * from regions", con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlregion.DataSource = ds;
ddlregion.DataTextField = "REGION_DESC";
ddlregion.DataValueField = "REGION_CODE";
ddlregion.DataBind();
}
protected void ddlregion_SelectedIndexChanged(object sender, EventArgs e)
{
ddlDepot.Items.Clear();
ddlDepot.Items.Add(new ListItem("--Select Depot--", ""));
for (int i = 0; i < ddlregion.Items.Count; i++)
{
if (ddlregion.Items[i].Selected == true)
{
string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr, depots d where r.region_code = sr.region_code and sr.sub_region_code = d.sub_region_code and active = 'Y' and d.depot_code = " + ddlregion.SelectedItem.Value + "";
OracleCommand Cmd = new OracleCommand(str, con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlDepot.DataSource = ds;
ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
ddlDepot.DataValueField = "DEPOT_CODE";
ddlDepot.DataBind();
}
}
}
Thanks

Looking at your code again it doesn't look like you need to loop through your region dropdown list. In your ddlRegion_indexchanged just go ahead and bind your ddlDepot drop down and pass it the ddlregion.SelectedValue. So all you need is below, the ddlDepot will bind accordingly when you change the Region because you pass it the selected value every time.
string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr, depots d where r.region_code = sr.region_code and sr.sub_region_code = d.sub_region_code and active = 'Y' and d.depot_code = " + ddlregion.SelectedValue + "";
OracleCommand Cmd = new OracleCommand(str, con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlDepot.DataSource = ds;
ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
ddlDepot.DataValueField = "DEPOT_CODE";
ddlDepot.DataBind();

Related

How to create SelectIndexChanged event for multiple selection in asp.net

I want to select multiple item from checkbox list and then fill another check Box list on select index changed event of First check box list but after one selection its not working for another selected items because of "Auto Post Back = true" while debbuging i can see pointer start from page load event. I want after selection of all Item it should be fire "ddlregion_SelectedIndexChanged" so that I can able see all selected Item related value in another check Box List.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRegion();
}
}
Due to this its not fetching data for another selected Items how to solve this issues Please help me.
ddlRegion Code Binding:
public void BindRegion()
{
OracleCommand Cmd = new OracleCommand("select * from regions", con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlregion.DataSource = ds;
ddlregion.DataTextField = "REGION_DESC";
ddlregion.DataValueField = "REGION_CODE";
ddlregion.DataBind();
}
Select Index Changed Event
protected void ddlregion_SelectedIndexChanged(object sender, EventArgs e)
{
ddlDepot.Items.Clear();
ddlDepot.Items.Add(new ListItem("--Select Depot--", ""));
foreach (ListItem item in ddlregion.Items)
{
if (item.Selected == true)
{
string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr, depots d where r.region_code = sr.region_code and sr.sub_region_code = d.sub_region_code and active = 'Y' and r.region_code in " + ddlregion.SelectedValue + "";
OracleCommand Cmd = new OracleCommand(str, con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlDepot.DataSource = ds;
ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
ddlDepot.DataValueField = "DEPOT_CODE";
ddlDepot.DataBind();
}
}
}
You need to build your region list for the SQL, then once done call your SQL and Bind.
string regions = "";
bool regionSelected = false;
foreach (ListItem item in ddlregion.Items)
{
if (item.Selected == true)
{
regions += item.SelectedValue + ",";
regionSelected = true;
}
}
// looping done so clean up string for SQL
regions = "(" + regions.TrimEnd(',') + " )";
//only run if a region is selected
if(regionSelected)
{
string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr,
depots d where r.region_code = sr.region_code and
sr.sub_region_code = d.sub_region_code and active = 'Y' and r.region_code
in " + regions;
OracleCommand Cmd = new OracleCommand(str, con);
Cmd.CommandType = CommandType.Text;
OracleDataAdapter da = new OracleDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = Cmd;
da.Fill(ds);
ddlDepot.DataSource = ds;
ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
ddlDepot.DataValueField = "DEPOT_CODE";
ddlDepot.DataBind();
}

i got an da.Fill(ds) error for my shopping card project in asp.net c#

public partial class AddToCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("sno");
dt.Columns.Add("ProductID");
dt.Columns.Add("ProductName");
dt.Columns.Add("Price");
dt.Columns.Add("ProductImage");
dt.Columns.Add("Cost");
dt.Columns.Add("TotalCost");
if (Request.QueryString["id"] != null)
{
if (Session["Buyitems"] == null)
{
dr = dt.NewRow();
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True";
SqlConnection scon = new SqlConnection(mycon);
String myquery = "select * from productdetail where ProductID=" + Request.QueryString["id"];
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = scon;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = 1;
dr["ProductID"] = ds.Tables[0].Rows[0]["ProductID"].ToString();
dr["ProductName"] = ds.Tables[0].Rows[0]["ProductName"].ToString();
dr["ProductImage"] = ds.Tables[0].Rows[0]["ProductImage"].ToString();
dr["Price"] = ds.Tables[0].Rows[0]["Price"].ToString();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
}
else
{
dt = (DataTable)Session["buyitems"];
int sr;
sr = dt.Rows.Count;
dr = dt.NewRow();
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True";
SqlConnection scon = new SqlConnection(mycon);
String myquery = "select * from productdetail where ProductID=" + Request.QueryString["id"];
SqlCommand cmd = new SqlCommand();
cmd.CommandText = myquery;
cmd.Connection = scon;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dr["sno"] = sr + 1;
dr["ProductID"] = ds.Tables[0].Rows[0]["ProductID"].ToString();
dr["ProductName"] = ds.Tables[0].Rows[0]["ProductName"].ToString();
dr["ProductImage"] = ds.Tables[0].Rows[0]["ProductImage"].ToString();
dr["Price"] = ds.Tables[0].Rows[0]["Price"].ToString();
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["buyitems"] = dt;
}
}
else
{
dt = (DataTable)Session["buyitems"];
GridView1.DataSource = dt;
GridView1.DataBind();
Update your connection string like below. You are missing to specify whether you want to use Windows Authentication or User Id & Password.
For Windows Authentication use Integrated Security=SSPI as below :
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=SSPI";
For Authentication with User Id & Password add User Id & Password as below. Use original user id and password. I have taken sa for example. :
String mycon = "Data Source=DESKTOP-8C66I6S/SQLEXPRESS;Initial Catalog=haritiShopping;Integrated Security=True;user id=sa;password=sa";
Also you need to open connection with
SqlConnection scon = new SqlConnection(mycon);
scon.Open(); //Open connection
P.S. It is always recommended to Close connection too. Add scon.Open(); after da.Fill(ds); line. Why always close Database connection?
.
da.Fill(ds);
scon.Close(); //Close connection

How to Implement Multiple search in Asp.Net

I want to Implement multiple search query system in Asp.Net where search input are in form of TEXTBOX and DROPDOWN LIST. Query should work in combination or indivisually to filter the data from SQL Server
and show in Gridview.
This Code Snippet is for filtering two Dropdown values:
if (Agree_type_srch.SelectedValue != null || Status_srch.SelectedValue != null)
{
if (Agree_type_srch.SelectedValue != null)
{
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand com = new SqlCommand("Select *from EntryDatabase where Agree_type ='" + Agree_type_srch.SelectedItem.Text + "'", conn);
SqlDataAdapter sqldatad = new SqlDataAdapter();
DataSet ds = new DataSet();
com.Connection = conn;
sqldatad.SelectCommand = com;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
else if (Status_srch.SelectedValue != null)
{
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand com = new SqlCommand("Select *from EntryDatabase where Curnt_St ='" + Status_srch.SelectedItem.Text + "'", conn);
SqlDataAdapter sqldatad = new SqlDataAdapter();
DataSet ds = new DataSet();
com.Connection = conn;
sqldatad.SelectCommand = com;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
if (Agree_type_srch.SelectedItem.Text != null && Status_srch.SelectedItem.Text != null)
{
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
SqlConnection conn = new SqlConnection(connString);
SqlCommand com = new SqlCommand("Select * from EntryDatabase where Agree_type ='" + Agree_type_srch.SelectedItem.Text + "'and Curnt_St ='" + Status_srch.SelectedItem.Text + "'", conn);
SqlDataAdapter sqldatad = new SqlDataAdapter();
DataSet ds = new DataSet();
com.Connection = conn;
sqldatad.SelectCommand = com;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
...
First, using string concatenation to provide parameters can result in SQL injection, use SqlParameter to pass parameters would be better.
Second, consider to warp all SqlClient classes by using scope so you don't have to worry close/dispose.
Lastly, For your question, you can use WHERE 1=1 then append any conditions you need.
Take your code as instance.
string connString = #"data source=ABC; database=XYZ; user id=sa; password=1234;";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
string query = "SELECT * FROM EntryDatabase WHERE 1=1";
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
if (Agree_type_srch.SelectedValue != null)
{
query += " AND Agree_type = #agree_type";
cmd.Parameters.AddWithValue("agree_type", Agree_type_srch.SelectedValue);
}
if (Status_srch.SelectedValue != null)
{
query += " AND Curnt_St = #curnt_st";
cmd.Parameters.AddWithValue("curnt_st", Status_srch.SelectedValue);
}
cmd.CommandText = query;
using (SqlDataAdapter sqldatad = new SqlDataAdapter())
{
DataSet ds = new DataSet();
sqldatad.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sqldatad.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}

Dropdown sets as System.Data.DataRow, What is wrong with my code?

System.Data.DataTable dt = new System.Data.DataTable();
CDbAccess db = new CDbAccess();
IDbConnection conn = db.GetConnectionInterface();
conn.Open();
string str = "select eName from bt_modules";
IDbCommand cmd = db.GetCommandInterface(str);
IDbDataAdapter da = db.GetDataAdapterInterface(cmd);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
DropDownList2.DataSource = dt;
DropDownList2.DataBind();
conn.Close();
The above code set Dropdown items as System.Data.DataRow
How to get the actual values?
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "eName";
DropDownList2.DataBind();
conn.close();
You can refer this code also http://www.aspdotnet-suresh.com/2012/10/showbind-data-to-aspnet-dropdownlist.html
try like this :
DropDownList2.DataSource = dt;
DropDownList1.DataTextField = "eName " '// Column Name
DropDownList2.DataBind();
If you want to use value field also using like this
DropDownList2.DataSource = dt;
DropDownList1.DataTextField = "eName " '// Column Name
DropDownList1.DataValueField = "eid" '// Column Name
DropDownList2.DataBind();

DataRow row = dt.Rows[0]; //There is no row at position 0 indexoutofrangeexception

SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["GVRAgencyConnectionString1"].ConnectionString);
con1.Open();
SqlDataAdapter da = new SqlDataAdapter("select weight,mrp_pack,no_of_pack,mrp_master_pack from dbo.purchase_order where product_ID=#product_ID",con1);
da.SelectCommand.Parameters.AddWithValue("#product_ID", DropDownList3.Text);
DataTable dt = new DataTable();
da.Fill(dt);
con1.Close();
DataRow row = dt.Rows[0];
txtwgt.Text = row["weight"].ToString();
txtmrpsinglepack.Text = row["mrp_pack"].ToString();
txtnoofmasterpack.Text = row["no_of_pack"].ToString();
txtmrpmaster.Text = row["mrp_master_pack"].ToString();
1) try this :
2)welcome to StackOverFlow.
3) you cant move to live with some girl if you dont have a girl.
Same here - you cant touch with records in datatable withindex 0 if you dont know that you already have rows in index 0.
SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["GVRAgencyConnectionString1"].ConnectionString);
con1.Open();
SqlDataAdapter da = new SqlDataAdapter("select weight,mrp_pack,no_of_pack,mrp_master_pack from dbo.purchase_order where product_ID=#product_ID",con1);
da.SelectCommand.Parameters.AddWithValue("#product_ID", DropDownList3.Text);
DataTable dt = new DataTable();
da.Fill(dt);
con1.Close();
if (dt.Rows.Count==0) return;
DataRow row = dt.Rows[0];
txtwgt.Text = row["weight"].ToString();
txtmrpsinglepack.Text = row["mrp_pack"].ToString();
txtnoofmasterpack.Text = row["no_of_pack"].ToString();
txtmrpmaster.Text = row["mrp_master_pack"].ToString();
Your result is empty, so there are no rows at all in the data table.
You can check this before trying to access the row:
if (dt.Rows.Count > 0) {
...
}
OracleConnection con = new OracleConnection(ConnectionString);
string selectquery = "SELECT TITLE,DESCRIPTION,SEVERITY,STATUS,IMPACT,USER_NAME AS CREATED_BY ,CREATED_DATE FROM Issues A, USERS B Where A.CREATED_BY=B.USER_ID AND A.ISSUE_ID=" + ID;
//string selectquery = "SELECT TITLE,DESCRIPTION,USER_NAME AS ASSIGNED_TO,SEVERITY,STATUS,IMPACT,USER_NAME AS CREATED_BY ,CREATED_DATE FROM Issues A, USERS B Where A.CREATED_BY=B.USER_ID AND A.ASSIGNED_TO=B.USER_ID AND A.ISSUE_ID=" + ID;
OracleDataAdapter adp = new OracleDataAdapter(selectquery, con);
DataTable dt = new DataTable();
adp.Fill(dt);
//GridView1.DataSource = dt;
// GridView1.DataBind();
lblTitle.Text = dt.Rows[0]["TITLE"].ToString();
lblDescription.Text = dt.Rows[0][1].ToString();
//lblSeverity.Text = dt.Rows[0][2].ToString();
lblStatus.Text = dt.Rows[0][3].ToString();
lblStatus.CssClass ="status"+ dt.Rows[0][3].ToString();
//lblImpact.Text = dt.Rows[0][4].ToString();
lblCreatedby.Text = dt.Rows[0][5].ToString();
lblcreateDate.Text = dt.Rows[0]["CREATED_DATE"].ToString();
//blCommentsDescriptionresult.Text = txtCommentdescription.Text;

Resources