I wanted to know other ways where you can access data from the database into the asp.net website that i'veI've created for displaying details and images. As well as looking for better ways to use those retrieved data for displaying in a webpageweb page. Also, kindly correct me if my explanation to the flow of my code is incorrect, I'm still a beginner. Lets say i have a table name called guitarItems and inside it are the following data.
id type brand model price
1 Guitar Ibanez ARz307 9000
2 Guitar Ibanez DT420 10000
3 Guitar Ibanez JBM100 18000
First thing i did, i createdLet's say I have a class where the constructor named GuitarItems would gettable name called guitarItems and set
the data retrieved frominside it are the sql connection, which i will show you later.following data:
id type brand model price
1 Guitar Ibanez ARz307 9000
2 Guitar Ibanez DT420 10000
3 Guitar Ibanez JBM100 18000
public int Id { get; set; }
public string Type { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public double Price { get; set; }
public string ItemImage1 { get; set; }
public string ItemImage2 { get; set; }
public string Description { get; set; }
public string NeckType { get; set; }
public string Body { get; set; }
public string Fretboard { get; set; }
public string Fret { get; set; }
public string Bridge { get; set; }
public string NeckPickup { get; set; }
public string BridgePickup { get; set; }
public string HardwareColor { get; set; }
public GuitarItems(int id, string type, string brand,string model, double
price, string itemimage1, string itemimage2, string description,
string necktype, string body, string fretboard, string fret, string
bridge, string neckpickup, string bridgepickup, string hardwarecolor)
{
Id = id;
Type = type;
Brand = brand;
Model = model;
Price = price;
ItemImage1 = itemimage1;
ItemImage2 = itemimage2;
Description = description;
NeckType = necktype;
Body = body;
Fretboard = fretboard;
Fret = fret;
Bridge = bridge;
NeckPickup = neckpickup;
BridgePickup = bridgepickup;
HardwareColor = hardwarecolor;
}
The first thing I did was to create a class where the constructor named GuitarItems would get and set the data retrieved from the SQL connection, which I will show you later.
public int Id { get; set; }
public string Type { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public double Price { get; set; }
public string ItemImage1 { get; set; }
public string ItemImage2 { get; set; }
public string Description { get; set; }
public string NeckType { get; set; }
public string Body { get; set; }
public string Fretboard { get; set; }
public string Fret { get; set; }
public string Bridge { get; set; }
public string NeckPickup { get; set; }
public string BridgePickup { get; set; }
public string HardwareColor { get; set; }
public GuitarItems(int id, string type, string brand,string model, double
price, string itemimage1, string itemimage2, string description,
string necktype, string body, string fretboard, string fret, string
bridge, string neckpickup, string bridgepickup, string hardwarecolor)
{
Id = id;
Type = type;
Brand = brand;
Model = model;
Price = price;
ItemImage1 = itemimage1;
ItemImage2 = itemimage2;
Description = description;
NeckType = necktype;
Body = body;
Fretboard = fretboard;
Fret = fret;
Bridge = bridge;
NeckPickup = neckpickup;
BridgePickup = bridgepickup;
HardwareColor = hardwarecolor;
}
Now nextNext is the code that will connect to the database to get the data. As you can see below, Thethe data retrieved from the database is being supplemented to the contructorconstructor that iI created previously so that we can use it to other classes in the project.
public static ArrayList GetGuitarItems(string itemCategory)
{
ArrayList list = new ArrayList();
string query = string.Format("SELECT * FROM guitarItems WHERE brand LIKE @brand");
try
{
conn1.Open();
command1.CommandText = query;
command1.Parameters.Add(new SqlParameter("brand", itemCategory));
SqlDataReader reader = command1.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string type = reader.GetString(1);
string brand = reader.GetString(2);
string model = reader.GetString(3);
double price = reader.GetDouble(4);
string itemimage1 = reader.GetString(5);
string itemimage2 = reader.GetString(6);
string description = reader.GetString(7);
string necktype = reader.GetString(8);
string body = reader.GetString(9);
string fretboard = reader.GetString(10);
string fret = reader.GetString(11);
string bridge = reader.GetString(12);
string neckpickup = reader.GetString(13);
string bridgepickup = reader.GetString(14);
string hardwarecolor = reader.GetString(15);
GuitarItems gItems = new GuitarItems(id, type, brand, model, price, itemimage1, itemimage2, description, necktype, body,
fretboard, fret, bridge, neckpickup, bridgepickup, hardwarecolor);
list.Add(gItems);
}
}
finally
{
conn1.Close();
command1.Parameters.Clear();
}
return list;
}
public static ArrayList GetGuitarItems(string itemCategory)
{
ArrayList list = new ArrayList();
string query = string.Format("SELECT * FROM guitarItems WHERE brand LIKE @brand");
try
{
conn1.Open();
command1.CommandText = query;
command1.Parameters.Add(new SqlParameter("brand", itemCategory));
SqlDataReader reader = command1.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string type = reader.GetString(1);
string brand = reader.GetString(2);
string model = reader.GetString(3);
double price = reader.GetDouble(4);
string itemimage1 = reader.GetString(5);
string itemimage2 = reader.GetString(6);
string description = reader.GetString(7);
string necktype = reader.GetString(8);
string body = reader.GetString(9);
string fretboard = reader.GetString(10);
string fret = reader.GetString(11);
string bridge = reader.GetString(12);
string neckpickup = reader.GetString(13);
string bridgepickup = reader.GetString(14);
string hardwarecolor = reader.GetString(15);
GuitarItems gItems = new GuitarItems(id, type, brand, model, price, itemimage1, itemimage2, description, necktype, body,
fretboard, fret, bridge, neckpickup, bridgepickup, hardwarecolor);
list.Add(gItems);
}
}
finally
{
conn1.Close();
command1.Parameters.Clear();
}
return list;
}
And then lastly, belowhere is the code for displaying the data retrieved from the database. As for this page, it is only designed to retrieve one ibanez product. Other products would have their own webpageweb page respectively. Now, iI used the GetGuitarItemsGetGuitarItems method from the previous example to specify the product that iI want to retrieve for this page into an ArrayListArrayList variable called itemDetailsitemDetails. Then i'mI'm using a foreachforeach loop with a reference variable for GuitarItems
called gListGuitarItems called gList to get the data from the ArrayList itemDetailsArrayList itemDetails. The reference variable gList wouldgList would then be used to implementing it in the htmlHTML tags for displaying.
public partial class Pages_GuitarItemsIbanezDetails1 : System.Web.UI.Page
{
private string guitarBrandType = "Ibanez";
private int x = 0;
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
public void FillPage()
{
ArrayList itemDetails = new ArrayList();
itemDetails = ConnectionClassGuitarItems.GetGuitarItems(guitarBrandType);
StringBuilder sb = new StringBuilder();
foreach (GuitarItems gList in itemDetails)
{
if (x != 0)
{
x++;
continue;
}
sb.Append(
string.Format(
@"<div class='guitarItemsDetailsWrapper'>
<div class='guitarItemsDetailsImage'>
<img runat='server' src='{3}' />
</div>
<div class='guitarItemsDetailsStyle'>
<h2>Name: </h2><p>{0} {1}</p>
<br/>
<h2>Price: </h2><p>${2}</p>
<br/>
<h2>Description: </h2><p>{4}</p>
<br/>
<h2>Neck Type: </h2><p>{5}</p>
<br/>
<h2>Body: </h2><p>{6}</p>
<br/>
<h2>Fretboard: </h2><p>{7}</p>
<br/>
<h2>Fret: </h2><p>{8}</p>
<br/>
<h2>Bridge: </h2><p>{9}</p>
<br/>
<h2>Neck Pickup: </h2><p>{10}</p>
<br/>
<h2>Bridge Pickup: </h2><p>{11}</p>
<br/>
<h2>Hardware Color: </h2><p>{12}</p>
<br/>
</div>
</div>", gList.Brand, gList.Model, gList.Price, gList.ItemImage2, gList.Description, gList.NeckType,
gList.Body, gList.Fretboard, gList.Fret, gList.Bridge, gList.NeckPickup, gList.BridgePickup, gList.HardwareColor));
if (x == 0)
{
break;
}
x++;
}
lblOutput.Text = sb.ToString();
}
public partial class Pages_GuitarItemsIbanezDetails1 : System.Web.UI.Page
{
private string guitarBrandType = "Ibanez";
private int x = 0;
protected void Page_Load(object sender, EventArgs e)
{
FillPage();
}
public void FillPage()
{
ArrayList itemDetails = new ArrayList();
itemDetails = ConnectionClassGuitarItems.GetGuitarItems(guitarBrandType);
StringBuilder sb = new StringBuilder();
foreach (GuitarItems gList in itemDetails)
{
if (x != 0)
{
x++;
continue;
}
sb.Append(
string.Format(
@"<div class='guitarItemsDetailsWrapper'>
<div class='guitarItemsDetailsImage'>
<img runat='server' src='{3}' />
</div>
<div class='guitarItemsDetailsStyle'>
<h2>Name: </h2><p>{0} {1}</p>
<br/>
<h2>Price: </h2><p>${2}</p>
<br/>
<h2>Description: </h2><p>{4}</p>
<br/>
<h2>Neck Type: </h2><p>{5}</p>
<br/>
<h2>Body: </h2><p>{6}</p>
<br/>
<h2>Fretboard: </h2><p>{7}</p>
<br/>
<h2>Fret: </h2><p>{8}</p>
<br/>
<h2>Bridge: </h2><p>{9}</p>
<br/>
<h2>Neck Pickup: </h2><p>{10}</p>
<br/>
<h2>Bridge Pickup: </h2><p>{11}</p>
<br/>
<h2>Hardware Color: </h2><p>{12}</p>
<br/>
</div>
</div>", gList.Brand, gList.Model, gList.Price, gList.ItemImage2, gList.Description, gList.NeckType,
gList.Body, gList.Fretboard, gList.Fret, gList.Bridge, gList.NeckPickup, gList.BridgePickup, gList.HardwareColor));
if (x == 0)
{
break;
}
x++;
}
lblOutput.Text = sb.ToString();
}
Also, you might have noticed i'mI'm using breakbreak and continuecontinue inside my foreachforeach. Because if iI don't, it will display everything that it retrieved from the database. The outcome would be three guitar products displayed in one page. Like iI said previously, one product should only be displayed in one page. So right now, based on the code above, I'm retrieving the data for product id no.1 1. If iI want to display product id no.2 2, i'dI'd change the ifif statement to this:
if (x != 1)
{
x++;
continue;
}
if (x == 1)
{
break;
}
if (x != 1)
{
x++;
continue;
}
if (x == 1)
{
break;
}
As of now, my website is working okay by using the codes presented above. But i'mI'm really not sure if this is the right way to do it. Kindly suggest the appropriate techniques to achieve the same goal as iI have explained above.