0

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

3 に答える 3

1

DataSetを使用するか、コレクション内のビジネスエンティティにデータを入力してから、コレクションにバインドする必要があります。

List<Category> cats = new List<Category>();

while (reader.Read()) 
{ 
   Category cat = new Category();

   // fill properties from DataReader
   cats.Add(cat);
} 

pcategory.DataSource = cats; 
pcategory.DataValueField = "Category"; 
pcategory.DataBind(); 
于 2012-05-05T16:48:54.683 に答える
0

各ブロックの「reader.read()」ステートメントは実際にはデータの最初の行を読み取っているため、DataSourceを設定すると、最初の行はすでに読み取られています。取り出してみてください

結果を1つのステップでバインドするのではなく、各結果を反復処理する場合は、「while(reader.Read())」を使用する必要があります。

そうは言っても、データセットの使用、ロジックの分離などに関するコメントは有効です。

于 2012-05-05T18:08:42.070 に答える
0

Sab0tr0n、2つのうちの1つが起こっているのではないかと思います。

1)ある種の「カテゴリの追加」アクションを実行した後、最初の項目が表示されないと言っている場合は、挿入が完了する前にドロップダウンが表示されている可能性があります。つまり、挿入をデータベースにコミットできるようにした後、再クエリを実行する必要があります。

また

2)この行にブレークポイントを設定します。

string connectionString = ConfigurationManager.ConnectionStrings["LakerBids"].ConnectionString; 

次に、connectionStringが正しいデータベースに接続されていることを確認します。データベースのテストまたはステージングを指す古い構成ファイルがこの種の混乱を引き起こすのを見てきました。

幸運を。これらが答えではない場合は、例を単純化するか、アプリケーションで何をし、問題が発生したかを正確に説明してください。

于 2012-05-05T16:49:19.143 に答える