Can anybody help me with the issue I’m seeing? For some reason when I run my page, I get my drop down lists to populate the data, however the first item in my database, per each SQL query, doesn’t get populated.
For example, my database table is:
Category
1 Books
2 Clothing
3 Toys
4 Household Items
my first query –
SELECT Category FROM ProductCategories
my drop down list gets populated with
Clothing
Toys
Household Items
I have 2 other drop down lists I’m populating and those are doing the same thing. Once I get this figured out, I’ll try to figure out the other problem I’m having with inserting the data in the database.
Thank you!
public partial class InsertItems : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection;
SqlCommand populateList;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["LakerBids"].ConnectionString;
connection = new SqlConnection(connectionString);
populateList = new SqlCommand("USE LakerBids SELECT Category FROM ProductCategories;" +
"USE LakerBids SELECT SubCategory FROM ProductSubCategories;" +
"USE LakerBids SELECT LName FROM Users", connection);
if (!IsPostBack)
{
try
{
connection.Open();
reader = populateList.ExecuteReader();
while (reader.Read())
{
pcategory.DataSource = reader;
pcategory.DataValueField = "Category";
pcategory.DataBind();
}
reader.NextResult();
while (reader.Read())
{
psubcategory.DataSource = reader;
psubcategory.DataValueField = "SubCategory";
psubcategory.DataBind();
}
reader.NextResult();
while (reader.Read())
{
user.DataSource = reader;
user.DataValueField = "LName";
user.DataBind();
}
reader.Close();
}
finally
{
connection.Close();
}
}
}
protected void AddItem(object sender, EventArgs e)
{
if (Page.IsValid)
{
SqlConnection connection;
SqlCommand insertData;
string connectionString = ConfigurationManager.ConnectionStrings["LakerBids"].ConnectionString;
connection = new SqlConnection(connectionString);
insertData = new SqlCommand("INSERT INTO Products (ProductName, ProductDesc, CategoryID, SubCatID, StatusID, UserID, ReservePrice, AuctionLength, BidID)" +
"VALUES (@ProductName, @ProductDesc, @CategoryID, @SubCatID, 1, @UserID, @ReservePrice, @AuctionLength, NULL)", connection);
insertData.Parameters.Add("@ProductName", System.Data.SqlDbType.NVarChar, 50);
insertData.Parameters["@ProductName"].Value = pname.Text;
insertData.Parameters.Add("@ProductDesc", System.Data.SqlDbType.NVarChar, 200);
insertData.Parameters["@ProductDesc"].Value = pdesc.Text;
insertData.Parameters.Add("@CategoryID", System.Data.SqlDbType.Int);
insertData.Parameters["@CategoryID"].Value = pcategory.SelectedIndex;
insertData.Parameters.Add("@SubCatID", System.Data.SqlDbType.Int);
insertData.Parameters["@SubCatID"].Value = psubcategory.SelectedIndex;
insertData.Parameters.Add("@UserID", System.Data.SqlDbType.Int);
insertData.Parameters["@UserID"].Value = user.SelectedIndex + 2;
insertData.Parameters.Add("@ReservePrice", System.Data.SqlDbType.Money);
insertData.Parameters["@ReservePrice"].Value = Convert.ToDecimal(reserveprice.Text);
insertData.Parameters.Add("@AuctionLength", System.Data.SqlDbType.Int);
insertData.Parameters["@AuctionLength"].Value = Convert.ToInt32(auctionlength.Text);
try
{
connection.Open();
insertData.ExecuteNonQuery();
Response.Redirect("Categories.aspx");
}
catch (Exception error)
{
dberror.Text = error.ToString();
}
finally
{
connection.Close();
}
}
}
}
You need to either use a DataSet or populate business entities within a collection and then bind to the collection.