How to display category and sub category in asp.net using database - query-string

I need to display item from category and sub category.
Ex. category is Mens and Subcategory is footwear, shirts, T-shirt, Ties.
So when the customer clicks "shirts", all shirts images should be displayed.
So can you share me the logic for the same in ASP.net.
i have tried the below code.
Product.aspx.cs
string qr;
string category;
protected void Page_Load(object sender, EventArgs e)
{
if(qr!=null && category!=null)
{
qr = Request.QueryString["category"].Trim();
category = Request.QueryString["sub_cat"].Trim();
//sub_category = Request.QueryString["sub_category"].Trim();
}
if (!IsPostBack)
{
disp();
}
}
Class1 cc = new Class1();
public void disp()
{
if (category == "'Fabrics'")
{
DataList1.DataSource = cc.dis_diplay("select pname, images from product_entry where category=" +qr).Tables[0];
DataList1.DataBind();
}
And the code for Product.aspx
'" title='Linen'>Linen

Entering your question into Google provided a step-by-step tutorial on the first page of results that seems to do what you want: http://www.nullskull.com/a/1555/using-datalist-to-list-categorysubcategory-with-expand--collapse-facility-via-javascript.aspx

Related

session to exchange data from one page to another

protected void Page_Load(object sender, EventArgs e)
{
if (Session["namereturn"] != null)
{
MVOH.Text = Session["namereturn"].ToString();
}
protected void NextPage_Click(object sender, EventArgs e1)
{
Session["name"] = Convert.ToInt32(MVOH.Text);
Response.Redirect("~/Solution.aspx");
}
I am having the above code in first page and trying to pass value from textbox (MVOH) to next page. In the next page I have the code below. It works for the first value but it does not work if i want to change the value in first page and pass it to the next page second time..So I think I have to use count for the nextPage Button but I don't know how to use that and I want to display this value to the text box till i close the browser..Its like the checkout system in Ecommerce where you can go back and change your details..
protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] != null)
{
TextBox1.Text = Session["name"].ToString();
Session["namereturn"] = TextBox1.Text;
}
}
protected void Button_Click(object sender, EventArgs e1)
{
TextBox1.Text = Session["namereturn"].ToString();
Response.Redirect("~/Details.aspx");
}
What you should techinically do:
You should be submitting the information from textbox1 to a postback action related to page1.cshtml then redirect to the action that will render page2.cshtml and pass the textbox1 information
General Example below:
Public ActionResult Page1()
{
return View();
}
[HttpPost]
Public ActionResult Page1(ViewModel model)
{
return RedirectToAction("Page2",new{ textbox1 = model.textbox1});
}
Public ActionResult Page2(string textbox1)
{
var model = new ViewModel;
model.Textbox2 = textbox1;
return View(model);
}
Based on your question this is what you want:
If do not want to carry it back to the server, you could use the sessionStorage or localStorage from html5 to hold said information from page1.cshtml to page2.cshtml
Reference:
http://www.w3schools.com/Html/html5_webstorage.asp

Confused about Linq query

I have created a simple db named Hospital and I have a few columns .I have filled first dropdown named drdoctors.And it works
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
fetchDoctors();
}
}
void fetchDoctors() {
HospitalEntities entitiy = new HospitalEntities();
List<Doctor> doc = entitiy.Doctors.ToList();
drDoctor.DataSource = doc;
drDoctor.DataTextField = "Name";
drDoctor.DataValueField = "DoctorNo";
drDoctor.DataBind();
}
What I want to do is fill the other dropdown with the this doctor's patients .
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id= Int32.Parse( drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
var query= from p in entities.Doctors
}
But linq queries are so complicated.How can i do this
This should about do it. Please note, this code wasn't tested and may contain minor errors.
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id= Int32.Parse( drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
var query= (from d in entities.Doctors
join m in entities.MedExams on d.DoctorNo equals p.DoctorNo
join p in entities.Patients on m.PatientNo equals p.PatientNo
where d.DoctorNo == id
select p).ToList();
//Populate Patients from query
}
Looking at your diagram, it looks you don't have foreign key relationships set up. I would highly recommend doing this (for uncountable reasons). But by doing this, you will be able to "join" on tables much more easily like this:
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Int32.Parse(drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
drDoctor.DataSource = entities.Doctors
.Where(x => x.DoctorNo == id)
.SelectMany(x => s.MedExams.Select(y => y.Patients));
drDoctor.DataBind();
}

Getting the available view for the sharepoint list and populating into the dropdownlist?

1. Here the sample code i tried for getting the available views for the
sharepoint list and populating into the dropdownlist
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);
foreach (SPWeb web in site.AllWebs)
{
SPListCollection spListCollection = web.GetListsOfType(SPBaseType.GenericList);
foreach (SPList splist in spListCollection)
{
ListNameList.Items.Add(splist.Title.ToString());
}
ListNameList.Items.Insert(0, "--Select--");
}
}
}
protected void ListNameList_SelectedIndexChanged(object sender,EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);
SPWeb web = site.OpenWeb();
SPList selectedlist = web.Lists[ListNameList.SelectedValue];
foreach (SPView views in selectedlist.Views)
{
ViewNameList.Items.Add(views.Views.ToString());
}
}
I created the views in the sharepoint list,but in the above code
while looping i got the viewnames properly but when populating into
the dropdown list the output i am getting like Microsoft.Sharepoint
instaed of viewnames.
Can you help me out of this?
I think you just need to edit the line where you add in the new Item to the ViewNameList to use view.Title
protected void ListNameList_SelectedIndexChanged(object sender,EventArgs e)
{
SPSite site = new SPSite(SPContext.Current.Site.Url);
SPWeb web = site.OpenWeb();
SPList selectedlist = web.Lists[ListNameList.SelectedValue];
foreach (SPView views in selectedlist.Views)
{
ViewNameList.Items.Add(views.Title);
}
}
You also may want to order the views if the collection is not returning them in the order that you want.

passing gridview particular row datas to another page in asp.net?

i want to set session in login page and want to pass one page girdview data to another page.
slno title author publication takenby
1 book1 author1 pub1 sendrequest (button)
Above image showing gridview. when i click the Request Book button(1st one). it will redirect to sendrequest.aspx page. and have to bring that row data (i.e)
slno=1
title=book1
Author=author1
publication=publ
i tried below code
user.aspx.cs
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string user = (string)(Session["User"]);
if (e.CommandName.Equals("SendRequestCmd"))
{
if (user == null)
{
Session["slno"] = null;
Session["bookname"] = null;
var clickedRow = ((Button)e.CommandSource).NamingContainer as GridViewRow;
// now access the cells like this
SlNo = clickedRow.Cells[0].Text;
Session["slno"] = SlNo.ToString();
BookName = clickedRow.Cells[1].Text;
Session["bookname"] = BookName.ToString();
}
}
}
sendquest.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string slno = "", bookname = "";
slno = (string)(Session["slno"]);
bookname = (string)(Session["bookname"]);
if (slno == null || bookname == null)
{
Response.Write("serial no: " + slno + "\nbookname: " + bookname);
}
}
protected void Logout()
{
Session.Clear();
}
but i got error in
slno=(string)(Session["slno"]);
bookname=(string)(Session["bookname"]);
why? anybody correct it?
else say better way?
Without posting your error it's difficult to see what the problem is. You could check that Sessions are enabled in your application.
Another way to do this (which may be better) is passing just the id/slno (via QueryString) to your other page, then perform a lookup to get your data at that stage.
I will surely create a type(Class) for booking
with slno title author publication properties and
public class Booking
{
public Booking()
{
//
// TODO: Add constructor logic here
//
}
public int slno { get; set; }
public string Title { get; set; }
public string author { get; set; }
public string publication { get; set; }
}
set all properties of object of booking type and pass that object in session
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string user = (string)(Session["User"]);
if (e.CommandName.Equals("SendRequestCmd"))
{
if (user == null)
{
Booking b = new Booking();
b.slno = clickedRow.Cells[0].Text;
b.Title = BookName.ToString();
Session["Booing"] = b;
}
}
}
and on receiving end just case that session value into that object again
and access each property just by using dot with object name .
Booking b=(Booking)Session["Booking"]
int slno=b.slno;
string Title=b.Title;
Without posting error not possible to give suggestion or correction , my answer is just how can you pass multiple values using session from one page to another .
Thanks

asp.net data entry form

I want to build a form where users can enter some data in a few text boxes and click the "Add" button and have the the data appear in a grid or something like it. They need to be able to enter multiple rows.
Then when they are done they can click the "Save" button to save the data to the database.
How do I get the data from from the text boxes into the "grid"?
EDIT
Here's what I have so far
protected void Page_Load(object sender, EventArgs e)
{
DataTable myDataTable = new DataTable();
DataColumn dc1 = new DataColumn("Employee");
myDataTable.Columns.Add(dc1);
DataColumn dc2 = new DataColumn("Category");
myDataTable.Columns.Add(dc2);
DataColumn dc3 = new DataColumn("Description");
myDataTable.Columns.Add(dc3);
DataColumn dc4 = new DataColumn("P/S/N");
myDataTable.Columns.Add(dc4);
DataColumn dc5 = new DataColumn("Hours");
myDataTable.Columns.Add(dc5);
DataColumn dc6 = new DataColumn("WeekEnding");
myDataTable.Columns.Add(dc6);
}
protected void btnAddToGrid_Click(object sender, EventArgs e)
{
DataRow row = myDataTable.NewRow();// i'm getting error here sayind myDataTable does not exist
row["Employee"] = LoginName1.ToString();
row["Category"] = ddlCategory.SelectedValue;
row["Description"] = txtDescription.Text;
row["P/S/N"] = ddlPSN.SelectedValue;
row["Hours"] = ddlHours.SelectedValue;
row["WeekEnding"] = txtWeekEnding.Text;
myDataTable.Rows.Add(row);
Ok your first problem from your comment:
i'm getting error here sayind myDataTable does not exist
Is because you defined your table in the Page_Load and then it goes out of scope at the end of the function. It sounds like you don't understand the basic concepts of ASP.NET and what you are trying to do.
Here is a quick and dirty untested solution to your problem but I must stress that you should try and understand why this solution works before trying to extend it or do more in ASP.NET. I hope my 10 minutes of my time helps you get a good start into understanding C# and ASP.NET.
[Serializable]
public class YourData
{
public string Employee { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public string P_S_N { get; set; }
public string Hours { get; set; }
public string WeekEnding { get; set; }
}
// Used to add your list of data to the viewstate cache
// (There are other ways to store data between posts but I am trying to keep it simple)
public void SetYourCachedData(List<YourData> data)
{
ViewState["YourDataCache"] = data;
}
// Used to get your save data so far
public List<YourData> GetYourCachedData()
{
return ViewState["YourDataCache"] == null ?
new List<YourData>() : (List<YourData>)(ViewState["YourDataCache"]);
}
protected void Page_Load(object sender, EventArgs e)
{
// Do nothing
}
protected void btnAddToGrid_Click(object sender, EventArgs e)
{
// Get the data and store it in the ViewState to cache it between post backs
// Assuming one record added with each click.
List<YourData> data = GetYourCachedData();
data.Add(new YourData()
{
Employee = LoginName1.ToString(),
Category = ddlCategory.SelectedValue,
Description = txtDescription.Text,
P_S_N = ddlPSN.SelectedValue,
Hours = ddlHours.SelectedValue,
WeekEnding = txtWeekEnding.Text
});
// You can bind to any type of collection easily.
// You don't need to use a DataTable with a GridView
yourGrid.DataSource = data;
yourGrid.DataBind();
SetYourCachedData(data);
}
protected void btnSaveData_Click(object sender, EventArgs e)
{
// Pull data from the ViewState cache
List<YourData> data = GetYourCachedData();
// Now iterate through data to save it to your database...
// look this up if you don't know how as this is a lot more work.
}
looks like you just need to set the output object's datasource and bind it.
I.e.:
myGrid.datasource = myDataTable
myGrid.databind()
And as Kelsey says -- keep your datatable in scope.
a public dim right above your page_load would be easier, if you just want to try it out. Otherwise, the separate class approach is a good way to go.

Resources