아무도 내가보고있는 문제를 해결할 수 있습니까? 어떤 이유로 내 페이지를 실행할 때 목록을 가져 와서 데이터를 채우지 만 각 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를 채우는 것이다. 코드 레이아웃을 사용하면 버그를 추적하기가 정말 어렵습니다. 이뿐만 아니라 일반적으로. – Oybek
왜 그 3 개의 선택을 결합하고 있습니까? 그것들을 분리 할 수있게해라. 카테고리에 따라서 "USE LakerBids SELECT Category from ProductCategories;" " – Thousand