任何人都可以幫我解決我看到的問題嗎?出於某種原因,當我運行我的頁面時,我得到我的下拉列表來填充數據,但是我的數據庫中的每個SQL查詢的第一項沒有被填充。沒有從SqlDataReader獲得所有結果
例如,我的數據庫表:
類別
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();
}
}
}
}
您是否期待每個查詢一行? –
我可以給你一個建議。你最好把你的邏輯分成幾個層次。至少兩個。首先是對數據庫進行查詢並將結果轉換爲內存對象然後填充UI的dal。使用您的代碼佈局,追蹤錯誤非常困難。不僅這一個,而且一般。 – Oybek
你爲什麼要結合這3個選擇?嘗試將它們分開,它更易於管理。 因此對於您的類別,只需使用「USE LakerBids SELECT Category FROM ProductCategories」;「」 – Thousand