简体   繁体   中英

Index was outside the bounds of the array when auto filling

i have dropdownlistProdID, Namelabel & Quantitytextbox.

DropDownList has data from my Database

&

When selectedindexchanged, it should fill the Namelabel

check image here

ERROR occurs when i click data from my dropdownlist

ERROR IMAGE

here is my code 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>  

here is CODE BEHIND

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);
        }

    }

THANKS GUYS! PLEASE HELP

You are reading only one field and only one value from database.

Why don't you use SqlCommand.ExecuteScalar

To fix this issue, can you change

Name.Text = dr.GetString(1);

to

Name.Text = dr.GetString(0);

As C# used 0 based indexes.

The problem is with your query. You're not even retrieving Name from database and expecting to show it.

Just fixed the code in your SelectedIndexChanged event handler:

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);
}

FIXED

By the help of Mr @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);
    }
}

just fixed the code from my SelectedIndexChanged

Thanks Guys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM