hello all friends,
guys please i need a small xcar stor project with smal data base containes some info about cars..and in site page.. that i can select all info about the car" using ADO" , no need for a form.. i just need the Dd and the code!
please if some one have sumthing like that..help me ! :)
Thanks in advance.
Wael1988 0 Newbie Poster
Dani AI
Generated
— below is a compact, ready-to-run example: a minimal SQL schema + sample inserts, a web.config connection string, ASP.NET WebForms markup (DropDownList) and C# ADO.NET code-behind that fills the dropdown and shows selected car details. The example uses ADO.NET (SqlClient), keeps connections in using blocks, and uses a parameterized query for the details lookup. Note: WebForms server controls must live inside a <form runat="server"> and BindCars() should run only when !IsPostBack.
SQL (create table + sample rows):
CREATE TABLE Cars (
CarID INT IDENTITY(1,1) PRIMARY KEY,
Make NVARCHAR(50) NOT NULL,
Model NVARCHAR(50) NOT NULL,
Year INT NOT NULL,
Price DECIMAL(10,2) NULL,
Color NVARCHAR(30) NULL,
Description NVARCHAR(500) NULL
);
INSERT INTO Cars (Make, Model, Year, Price, Color, Description) VALUES
('Toyota','Corolla',2012,7999.00,'Silver','Reliable compact sedan'),
('Ford','Mustang',2015,19999.00,'Red','V6 coupe'),
('BMW','X3',2018,25999.00,'Black','Compact luxury SUV'); web.config connection string:
<connectionStrings>
<add name="CarDb" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=CarStore;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings> ASPX snippet (inside <form runat="server">):
<asp:DropDownList ID="ddlCars" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCars_SelectedIndexChanged" />
<asp:Label ID="lblDetails" runat="server" /> C# code-behind (core parts):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) BindCars();
}
private void BindCars()
{
string cs = ConfigurationManager.ConnectionStrings["CarDb"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
using (SqlCommand cmd = new SqlCommand("SELECT CarID, Make + ' ' + Model + ' (' + CAST(Year AS VARCHAR(4)) + ')' AS Title FROM Cars ORDER BY Make, Model", conn))
{
conn.Open();
ddlCars.DataSource = cmd.ExecuteReader();
ddlCars.DataTextField = "Title";
ddlCars.DataValueField = "CarID";
ddlCars.DataBind();
}
ddlCars.Items.Insert(0, new ListItem("-- Select --","0"));
}
protected void ddlCars_SelectedIndexChanged(object sender, EventArgs e)
{
int id;
if (!int.TryParse(ddlCars.SelectedValue, out id) || id == 0) { lblDetails.Text = ""; return; }
string cs = ConfigurationManager.ConnectionStrings["CarDb"].ConnectionString;
using (SqlConnection conn = new SqlConnection(cs))
using (SqlCommand cmd = new SqlCommand("SELECT Make, Model, Year, Price, Color, Description FROM Cars WHERE CarID = @id", conn))
{
cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = id;
conn.Open();
using (SqlDataReader r = cmd.ExecuteReader())
{
if (r.Read())
{
lblDetails.Text = string.Format("{0} {1} ({2}) - {3:C}<br/>{4}", r["Make"], r["Model"], r["Year"], r["Price"], r["Description"]);
}
}
}
} Quick troubleshooting / tips:
- Ensure
BindCars()runs only when!IsPostBackto avoid losing selection. AutoPostBack="true"is required to fireSelectedIndexChanged.- Use parameterized queries (shown) to avoid injection; prefer typed parameters over
AddWithValue. - Verify the connection string, SQL Server service, and that the
CarStoreDB exists. - For modern projects consider ORMs (Entity Framework, Dapper) but for plain ADO usage the above is concise.
Reference: ADO.NET overview
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.