自动填充时索引超出数组的范围
本文关键字:数组 范围 索引 填充 | 更新日期: 2023-09-27 18:33:06
i have dropdownlistProdID, Namelabel & Quantitytextbox.
下拉列表包含来自我的数据库的数据
&
选择索引更改时,它应该填写名称标签
在此处查看图像
单击下拉列表中的数据时发生错误
错误图像
这是我的代码ASP
<table >
<tr>
<td>PO#</td>
<td>Product#</td>
<td class="style2">
Product Name
</td>
<td>
Quantity
</td>
</tr>
<tr>
<td>
<asp:Label ID="POID" runat="server" BorderColor="Black" Font-Size="Larger" />
</td>
<td>
<asp:DropDownList ID="ddlProdID" runat="server" class="form-control"
AutoPostBack="True" onselectedindexchanged="ddlProdID_SelectedIndexChanged"></asp:DropDownList>
</td>
<td class="style2">
<asp:Label ID="Name" runat="server"></asp:Label>
</td>
</td>
<td>
<asp:TextBox ID="Quantity" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3"></td>
<td class="style2"></td>
<td>
</td>
<td>
<asp:Button ID="AddProduct" runat="server" Text="Add Product"
BackColor="#999966" onclick="AddProduct_Click" /></td>
</tr>
这是代码隐藏
public partial class PODetails : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(Helper.GetCon());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AddProducts();
GetPO();
int GetProductID;
int GetSupplierID;
LoadOptions();
}
}
protected void LoadOptions()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT ProductID FROM Products";
cmd.Parameters.AddWithValue("@ProductID", ddlProdID.SelectedValue);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable Products = new DataTable();
da.Fill(Products);
ddlProdID.DataSource = Products;
ddlProdID.DataTextField = "ProductID";
ddlProdID.DataValueField = "ProductID";
ddlProdID.DataBind();
}
protected void ddlProdID_SelectedIndexChanged(object sender, EventArgs e)
{
string ProductID = ddlProdID.SelectedItem.Value;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT ProductID FROM Products";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
Name.Text = dr.GetString(1);
}
}
谢谢大家!请帮助
您只读取一个字段,只读取数据库中的一个值。
你为什么不使用SqlCommand.ExecuteScalar
要解决此问题,您可以更改吗
Name.Text = dr.GetString(1);
自
Name.Text = dr.GetString(0);
由于 C# 使用基于 0 的索引。
问题出在您的查询上。您甚至没有从数据库中检索Name
并期望显示它。
刚刚修复了SelectedIndexChanged
事件处理程序中的代码:
cmd.CommandText = "SELECT ProductID, NAME FROM Products WHERE ProductID = @ProductID"; //change column name as you have in database
cmd.Parameters.AddWithValue("@ProductID", ddlProdID.SelectedValue); //get data for specific product
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
Name.Text = dr.GetString(1);
}
固定
在@Shaharyar先生的帮助下
protected void ddlProdID_SelectedIndexChanged(object sender, EventArgs e)
{
string ProductID = ddlProdID.SelectedItem.Value;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT ProductID, Name FROM Products WHERE ProductID=@ProductID";
cmd.Parameters.AddWithValue("@ProductID", ddlProdID.SelectedValue);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows) {
dr.Read();
Name.Text = dr.GetString(1);
}
}
刚刚修复了我的选定索引更改的代码
谢谢大家