1

In my data base i have made a table with columns

ProductId                int            Unchecked
[Sports Name]            nvarchar(50)   Checked
Category                 nvarchar(50)   Checked
Brand                    nvarchar(50)   Checked
Image                    text           Checked
MRP                      varchar(50)    Checked
[Our Price]              varchar(50)    Checked
[You Save]               varchar(50)    Checked
[Shipping Charges]       varchar(50)    Checked
[Expected Delivery]      varchar(50)    Checked

and on the front hand i have create many linkbuttons such as badminton, tennis , table tennis etc.. and a drop downlist to choose the equipment wanted such as racket and shuttle for badminton, bat and ball for the cricket and so on after that a dropdownlist to choose its brand.

Now I want them to display the products as choosen by customer on the click and selection event only in one page not by creating page for each category Some what like this (the coding is not exact, it is just sample that what i want)

if(badminton.click==true)
{
  cmd.text= seclect * from products where [Sports Name == Badminton] And Category==setectedcategory and so on 
} 
else if(tennis.click==true
{
}
...........so on

or only in one statement just like

cmd.text= select * from products where [Sports Name]==LinkButton.click And all like this

Now please help me if u understand what i want.

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
  • 1
    **Side note:** Your table, this way isn't [***normalized***](http://en.wikipedia.org/wiki/Database_normalization). You should split this table `Products` into: `Products:ProductId, categoryId, BrandId, ..`, `Categories`, `Sports` and `Brands`. This will make your queries much more easier. – Mahmoud Gamal Nov 24 '12 at 04:01
  • yaa i know that thank u so much – Vandana Jain Nov 24 '12 at 04:24
  • Hey but by this i think the work will get increased as for insertion i used the statement "Insert into Products ([Sports Name],Category,Brand,Image,MRP,[Our Price],[You Save],[Shipping Charges],[Expected Delivery]) values('" + SportsName.SelectedItem + "','" + Category.SelectedItem + "','" + Brand.SelectedItem + "','" + next + sImageFileExtension + "','" + mrp.Text.ToString() + "','" + ourprice.Text.ToString() + "','" + yousave.Text.ToString() + "','" + shippingcharges.Text.ToString() + "','" + expecteddelivery.Text.ToString() + "')";so after normalizing how i will use this insertion statement – Vandana Jain Nov 24 '12 at 04:40
  • **Second side note:** If you normalized this data, you would add the ID (instead of the text) of the related categoryID, productID, etc. If you are using dropdown lists, set the displayField to the product name, and the valueField to the productID. – Marcus Nov 24 '12 at 05:57

1 Answers1

0

There are some problems in your database, I have to point to:

  • Don't use spaces in the columns' names, such as [Our Price], [Expected Delivery]. You can, but it is recommended not to do this. Your identifiers should comply to default rules for regular identifiers.

  • For monetary or currency values, use MONEY, SMALLMONEY1 it has many benifits, or even DECIMAL in some cases. Not NVARCHAR the way you did in your Products table.

  • Use Date and Time data types instead of NVARCHAR like in [Expected Delivery]. And the most important one:

  • Your database isn't normalized this way. You should create a separate tables for Cateogries, Sports and Brands.

For instance:

CREATE TABLE Equipments
(
  EquipmentId   INT NOT NULL IDENTITY(1, 1) 
                CONSTRAINT PK_EquipmentId PRIMARY KEY,
  EquipmentName NVARCHAR(50) NOT NULL
); 

CREATE TABLE Sports
(
   SportId   INT NOT NULL IDENTITY(1,1) 
             CONSTRAINT PK_SportId PRIMARY KEY,
   SportName NVARCHAR(50)
);

CREATE TABLE SportsEquipments 
(
   Id          INT NOT NULL IDENTITY(1, 1)<sup>2</sup> 
               CONSTRAINT PK_SportsEquipmentId PRIMARY KEY,
   SportId     INT NOT NULL 
               CONSTRAINT FK_SportId_Sports FOREIGN KEY 
               REFERENCES Sports(SportId),
   EquipmentId INT NOT NULL 
               CONSTRAINT FK_EquipmentId_Equipments FOREIGN KEY 
               REFERENCES Equipments(EquipmentId)
);

CREATE TABLE Categories
(
   CategoryId   INT NOT NULL IDENTITY(1, 1) 
                CONSTRAINT PK_CategoryId PRIMARY KEY,
   CategoryName NVARCHAR(50)
);

CREATE TABLE Brands
(
   BrandId   INT NOT NULL IDENTITY(1, 1) 
             CONSTRAINT PK_BrandId PRIMARY KEY,
   BrandName NVARCHAR(50)
);

CREATE TABLE Products
(
  ProductId        INT NOT NULL IDENTITY(1, 1) 
                   CONSTRAINT PK_ProductID PRIMARY KEY,
  SportId          INT NOT NULL 
                   CONSTRAINT FK_SportId_Products 
                   FOREIGN KEY REFERENCES Sports(SportId),
  CategoryId       INT NOT NULL 
                   CONSTRAINT FK_CategoryId_Categories 
                   FOREIGN KEY REFERENCES Categories(CategoryId),
  BrandId          INT NOT NULL 
                   CONSTRAINT FK_BrandId_Brands 
                   FOREIGN KEY REFERENCES Brands(BrandId),
  "Image"          TEXT,
  MRP              NVARCHAR(50),
  OurPrice         MONEY,
  YouSave          MONEY,
  ShippingCharges  MONEY,
  ExpectedDeliveryDate DATETIME
);

enter image description here

How Can I insert into the tables this way?

Later, when inserting into the Products table, in the insert from, you should display the list of sports, categories and brands as DropDownLists. But, in this drop down list, you only display the name of the entity. You only display the CategoryName in the Categories dropdown, BrandName in the Brands dropdown and so on. And you can do this using the following two properties of the drop down list:

Something like:

dropdown.DataSource = dataset;
dropdown.DataValueField  = "CategoryId";
dropdown.DataTextField = "CategoryName";
dropdown.DataBind();
//the same for other entities

Then you should insert only the selected id from the drop down list, and your INSERT statement would look something like:

INSERT INTO Products (SportId, CategoryId, BrandId, ... )
VALUES
(1, 2, 3, ...); 

But How can I Select and Search?

The same in the Search form, you should display these entities as drop down list then search for the products with the id selected by the drop down list:

SELECT ProductId, Name, ...
FROM Products
Where CategoryId = @SelectedCategoryId
 AND SportId = @selectedSportId
...

The same with your current problem, you can dynamically created link buttons from these entities, the categories list and sports list as well as other entities. This can be done using the GridView control, then create a data source and bind it to this grid view. With an <ItemTemplate> as <asp:LinkButton> and bind it to the column name selected by your query. If you googled for this, you will find a lot of threads explaining how you can do this. For example :

Hope this is what are you looking for.


  • 1:In some cases, may be you shouldn't use it.

  • 2:In the SportsEquipments, I used the Id as a primary key, this way you any equipment can be referenced by many sports. If you want to make the equipment unique for each sports make a composite key(SportId, EquipmentId) instead.

Community
  • 1
  • 1
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164