I am trying to use selectedItem from the dropdown menu in my SQL statement to populate a text field in c # asp.net

So far, everything loads without errors, and SQL calls will be replaced by more secure stored procedures when everything works. When I select a furnace from the drop-down menu, it should go to the method and fill in the text box with the run number. But when I choose something, it only returns to the first index 10A. I also coded that index 0 should say “Select Furnace”, but that also does not appear, but only the first index. Are there any suggestions on how to grab the selected index and populate the text box from the SQL query?

 <asp:Content ID="BodyContent" ContentPlaceHolderID="BodyPlaceHolder" runat="server">
  <center>
   <table style="text-align:left">
    <tr>
     <td align="right">
            <asp:Label ID="FurnaceID" Text="JUMP TO FURNACE : " runat="server" />
        </td>
        <td></td>
        <td>
    <asp:DropDownList ID="FurnID" runat="server" AutoPostBack ="true"
        onselectedindexchanged="FurnID_SelectedIndexChanged" Width="150px">
        <asp:ListItem Text="Select Furnace" Value = "0" />
    </asp:DropDownList>

Behind the code:

LoadList (); called in the page_load event

   public void LoadList()
    {

            SqlCommand cmd = new SqlCommand("SELECT * FROM [Furnace]", new SqlConnection(ConfigurationManager.ConnectionStrings["FurnaceDeckConnectionString"].ConnectionString));
            cmd.Connection.Open();

            SqlDataReader Furns;
            Furns = cmd.ExecuteReader();


            FurnID.DataSource = Furns;

            FurnID.DataValueField = "Furnaceno";
            FurnID.DataTextField = "Furnaceno";
            FurnID.DataBind();

            cmd.Connection.Close();
            cmd.Connection.Dispose();

    }

When the index changes, this method is called:

protected void FurnID_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            String selectedFurn = "";
            ListItem selectFurn = FurnID.Items.FindByText(selectedFurn);
            LoadFurnDetails(selectedFurn);
        }

    }

    public void LoadFurnDetails(String F)
    {
        String selectF = F;
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand("SELECT * FROM [Furnace Run Data Table] Where Furnace = 'selectF' and Completed = 0 ", new SqlConnection(ConfigurationManager.ConnectionStrings["FurnaceDeckConnectionString"].ConnectionString));
        cmd.Connection.Open();
        SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
        sqlDa.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            lblFurnId.Text = dt.Rows[0]["runno"].ToString();


        }

        cmd.Connection.Close();
        cmd.Connection.Dispose(); 

    }
+4
4

    if (IsPostBack)
    {
        String selectedFurn = "";
        ListItem selectFurn = FurnID.Items.FindByText(selectedFurn);
        LoadFurnDetails(selectedFurn);
    }

... "selectedFurn" . . , ... , ListItem, ...

, ? ListItem .Selecteditem, LoadFurnDetails...

:

SqlCommand cmd = new SqlCommand("SELECT * FROM [Furnace Run Data Table] Where Furnace = 'selectF' and Completed = 0 " ...

, "selectF" , "selectF" ... "selectF" .

no-go: !

+1

@Schnugo, , , . .

         if (IsPostBack)
        {

            String selectFurn = FurnID.SelectedItem.Text.ToString();
            SqlConnection m_sqlConnection;
            string m_connectionString = ConfigurationManager.ConnectionStrings["FurnaceDeckConnectionString"].ConnectionString;
            using (m_sqlConnection = new SqlConnection(m_connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("Load_Furn"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@selectFurn",  selectFurn);
                        cmd.Connection = m_sqlConnection;
                        m_sqlConnection.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.HasRows)
+1

A datareader while . SqlDataAdapter, dataset, . , , .

0
source

Load the data into a table, and then use the table as data.

Try the following:

public void LoadList()
{
    var table = new System.Data.DataTable("Furnaceno");
    using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["FurnaceDeckConnectionString"].ConnectionString))
    {
        conn.Open();
        using (var cmd = new SqlCommand("SELECT * FROM [Furnace]", conn))
        {
            table.Load(cmd.ExecuteReader());
        }
    }
    FurnID.DataSource = table.DefaultView;
    FurnID.DataValueField = "Furnaceno";
    FurnID.DataTextField = "Furnaceno";
    FurnID.DataBind();
}
0
source

Source: https://habr.com/ru/post/1626581/


All Articles