How to create SelectIndexChanged event for multiple selection in asp.net - 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();
}

Related

Cascading for multiple selection of checkbox list

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();

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

Get the value of textbox in crystal report

I have crystal report, in which I need to get the value of textbox in crystal report. If I put textbox1= 1 then in crystal report it display as 1. How can I do this?
This is my crystal report code in default.aspx page.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "9";
TextBox2.Text = "02/02/2015";
TextBox3.Text = "02/03/2015";
DataTable dt = new DataTable();
String str = "select * from tbl1 where br=#search and Date between #search1 and #search2 ";
SqlCommand xp = new SqlCommand(str, con);
xp.Parameters.Add("#search", SqlDbType.VarChar).Value = TextBox1.Text;
xp.Parameters.Add("#search1", SqlDbType.VarChar).Value = TextBox2.Text;
xp.Parameters.Add("#search2", SqlDbType.VarChar).Value = TextBox3.Text;
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
crystalReport.SetDatabaseLogon("xx", "xxxx");
crystalReport.SetDataSource(dt);
CrystalReportViewer1.ReportSource = crystalReport;
CrystalReportViewer1.DataBind();
}
Here I need to display textbox1,2 and 3 value in crystal report
At first you have to create 3 parameters in CrystalReport toolbox under Parameter Fields as shown in the following figure.
Then you need to pass the values from your behind code
var rpt=new ReportDocument();
rpt.SetParameterValue("Search1","I am search1");
rpt.SetParameterValue("Search2","I am search2");
rpt.SetParameterValue("Search3","I am search3");
CrystalReportViewer1.ReportSource = rpt;
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "9";
DataTable dt = new DataTable();
String str = "select * from tbl1 where br=#search and Date between #search1 and #search2 ";
SqlCommand xp = new SqlCommand(str, con);
xp.Parameters.Add("#search", SqlDbType.VarChar).Value = TextBox1.Text;
xp.Parameters.Add("#search1", SqlDbType.VarChar).Value = TextBox2.Text;
xp.Parameters.Add("#search2", SqlDbType.VarChar).Value = TextBox3.Text;
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
ReportDocument crystalReport = new ReportDocument();
crystalReport.SetParameterValue("search", "I am search");
crystalReport.Load(Server.MapPath("CrystalReport.rpt"));
crystalReport.SetDatabaseLogon("xx", "xxxx");
crystalReport.SetDataSource(dt);
CrystalReportViewer1.ReportSource = crystalReport;
CrystalReportViewer1.DataBind();
}
}
I have added the parameter name as search.
but it shows error invalid report path. If I remove this line crystalReport.SetParameterValue("search", "I am search"); then its working.

how to add data in the row from arraylist in asp.net?

i have a database..i am getting my database values in myarraylist..now i want to add this in my datatable column and finally bind it to gridview and show data in webpage..
mycodebehind page
protected void Page_Load(object sender, EventArgs e)
{
ArrayList myArrayList = ConvertDataSetToArrayList();
// Display each item of ArrayList
DataTable dt = new DataTable();
dt.Columns.Add("User Id", Type.GetType("System.String"));
dt.Columns.Add("Problem Name", Type.GetType("System.String"));
dt.Columns.Add("Status", Type.GetType("System.String"));
for (int i = 0; i < 2; i++)
{
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
public ArrayList ConvertDataSetToArrayList()
{
string con = " ";
con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection objsqlconn = new SqlConnection(con);
objsqlconn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM usertable", objsqlconn);
cmd.ExecuteNonQuery();
cmd.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = cmd;
DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);
ArrayList myArrayList = new ArrayList();
foreach (DataRow dtRow in myDataSet.Tables[0].Rows)
{
myArrayList.Add(dtRow);
}
objsqlconn.Close();
return myArrayList;
}
i have problem in the for loop ..here how i will add rows and values to the columns from arraylist...
Do you mean something like this?
for (int i = 0; i < myArrayList.Count; i++)
{
var row = dt.NewRow();
row[0] = ((DataRow)myArrayList[i])[0];
row[1] = ((DataRow)myArrayList[i])[1];
row[2] = ((DataRow)myArrayList[i])[2];
dt.Rows.Add(row);
}
where indexes 0,1,2... can be replaced by column names "User Id", "Problem Name" and "Status"

What is wrong with my query in asp.net?

I am trying to fill a gridview by taking data from multiple tables. Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = Userfunctions.GetConnectionString();
con.Open();
string query = "SELECT RegisterTable.CourseCode,
RegisterTable.courseNumber,
RegisterTable.Term,RegisterTable.Grade,
CourseTable.CourseName,
CourseTable.Level,
CourseTable.Credit
FROM RegisterTable,CourseTable
WHERE StudentID='" + MyGlobals.currentID + "' and
RegisterTable.CourseCode=CourseTable.CourseCode and
RegisterTable.CourseNumber=CourseTable.CourseNumber and
RegisterTable.Term=CourseTable.Term";
SqlDataAdapter adap = new SqlDataAdapter(query, con);
DataTable tab = new DataTable();
adap.Fill(tab);
GridView1.DataSource = tab;
GridView1.DataBind();
}
This gives an error saying that "Incorrect syntax near the keyword 'where'." Can anyone help me with this? Thanks
EDIT:
Ignore the string's lack of concatenation. It was all on one line and you had to scroll for a mile to see it all. They just made it easier to see.
Have you tried re-writing your query (so it's not using a cross-join)?
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = null;
SqlCommand cmd = null;
SqlDataAdapter adap = null;
string query = string.Empty();
DataSet ds = null;
DataTable tab = null;
con = new SqlConnection();
con.ConnectionString = Userfunctions.GetConnectionString();
query = "SELECT RegisterTable.CourseCode, RegisterTable.CourseNumber, RegisterTable.Term, RegisterTable.Grade, CourseTable.CourseName, CourseTable.Level, CourseTable.Credit FROM RegisterTable INNER JOIN CourseTable ON RegisterTable.CourseCode = CourseTable.CourseCode AND RegisterTable.CourseNumber = CourseTable.CourseNumber AND RegisterTable.Term = CourseTable.Term WHERE StudentID = #StudentID;";
cmd = new SqlCommand(query, con);
cmd.Parameters.Add("StudentID", SqlDbType.VarChar, 50).Value = MyGlobals.currentID;
ds = new DataSet();
adap = new SqlDataAdapter(cmd);
adap.Fill(ds);
if (ds.Tables.Count > 0) {
tab = ds.Tables(0);
}
GridView1.DataSource = tab;
GridView1.DataBind();
}

Resources