有人可以帮我解决我看到的问题吗?出于某种原因,当我运行我的页面时,我得到了我的下拉列表来填充数据,但是我的数据库中的第一项,每个 SQL 查询,都没有被填充。
例如,我的数据库表是:
类别
1 Books
2 Clothing
3 Toys
4 Household Items
我的第一个查询-
SELECT Category FROM ProductCategories
我的下拉列表填充了
Clothing
Toys
Household Items
我还有另外 2 个要填充的下拉列表,它们正在做同样的事情。一旦我弄清楚了这一点,我将尝试找出在数据库中插入数据时遇到的另一个问题。
谢谢!
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();
}
}
}
}