誰かが私が見ている問題で私を助けることができますか?何らかの理由でページを実行すると、ドロップダウンリストが表示されてデータが入力されますが、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();
}
}
}
}