locked
Listview Bind Textbox with value from stored procedure RRS feed

  • Question

  • User444756084 posted

    I have a Listview that has 2 dropdowns and a textbox that needs to display a value based on the dropdowns. I have a stored procedure that takes the values of the dropdowns and should display the value from the stored procedure in the textbox. I am not getting an error. I am just not getting anything to display. I know my stored procedure is correct and returns the correct value. Does anyone see what I am doing wrong? I am use to MVC so picking up a webform app is new to me. 

       <td>
                                <asp:DropDownList runat="server" ID="ddlSubject" DataSourceID="sdsSubjects" DataTextField="Name" DataValueField="Subject" AutoPostBack="true" ToolTip="Select Department." /> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Department required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="ddlSubject" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator>
                      <asp:SqlDataSource runat="server" ID="sdsSubjects" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
                                SelectCommand="GetDepartments" SelectCommandType="StoredProcedure">
                               
                            </asp:SqlDataSource>
                            </td>
                            <td>
                                 <asp:DropDownList runat="server" ID="ddlClass" DataSourceID="sdsClasses" DataTextField="Name" DataValueField="CLASS_NBR" AutoPostBack="true" ToolTip="Select Class."  onselectedindexchanged="ddlClass_SelectedIndexChanged" /><asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="Class number required" Display="Dynamic" ValidationGroup="AddEval" ControlToValidate="ddlClass" Font-Bold="True" ForeColor="#CC0000"></asp:RequiredFieldValidator>
                      <asp:SqlDataSource runat="server" ID="sdsClasses" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
                                SelectCommand="GetCourseNumber" SelectCommandType="StoredProcedure">
                                <SelectParameters>
                                    
                                    <asp:ControlParameter Name="Subject" ControlID="ddlSubject" Type="String" />
                                 
                                </SelectParameters>
                            </asp:SqlDataSource>
                            </td>
                            <td>
                                <asp:TextBox ID="HrsTextBox" runat="server" Width="50px"   Text='<%# Bind("Hrs") %>' placeholder="Hours" />

    Code Behind:

      protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            DropDownList ddlSubject = (DropDownList)sender;
            DropDownList ddlClass = (DropDownList)sender;
            ListViewItem item1 = (ListViewItem)ddlSubject.NamingContainer;
            ListViewItem item2 = (ListViewItem)ddlClass.NamingContainer;
            DropDownList ddlgetSubject = (DropDownList)item1.FindControl("ddlSubject");
            DropDownList ddlgetClass = (DropDownList)item2.FindControl("ddlClass");
       
            string SubjectValue = ddlgetSubject.SelectedValue;
            string CourseValue = ddlgetClass.SelectedValue;
    
            
            TextBox hrs = ListView3.FindControl("hrs") as TextBox;
    
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager
                        .ConnectionStrings["MyConnectionString"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                   
                    cmd.CommandText = "GetCourseHrs";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Subject", SubjectValue.ToString());
                    cmd.Parameters.AddWithValue("@CLASS_NBR", CourseValue.ToString());
                  
                
                    cmd.Connection = conn;
                    conn.Open();
                   
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            if (sdr.HasRows)
                            {
    
                                hrs.Text = sdr["Credit"].ToString();
    
                            } 
                          
    
                        }
                    }
                    conn.Close();
                   
                }
            }
    
           
           
        }

    Thursday, August 1, 2019 3:09 PM

Answers

  • User-719153870 posted

    Hi gordon1221,

    As you said, your stored procedure is absolutely correct.

    But i found that you can't actually get the HrsTextBox with: TextBox hrs = ListView3.FindControl("hrs") as TextBox;

    Cause there's no textbox whose id is hrs in your listview3.

    Please refer to below code to modify yours:

    protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlClass = (DropDownList)sender;
                ListViewItem item1 = (ListViewItem)ddlClass.NamingContainer;
                DropDownList ddlgetSubject = (DropDownList)item1.FindControl("ddlSubject");
                DropDownList ddlgetClass = (DropDownList)item1.FindControl("ddlClass");
                TextBox hrs = (TextBox)item1.FindControl("HrsTextBox");
    
                string SubjectValue = ddlgetSubject.SelectedValue;
                string CourseValue = ddlgetClass.SelectedValue;
    
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DConString"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "GetCourseHrs";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Subject", SubjectValue.ToString());
                        cmd.Parameters.AddWithValue("@CLASS_NBR", CourseValue.ToString());
                        
                        cmd.Connection = conn;
                        conn.Open();
    
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                if (sdr.HasRows)
                                {
                                    hrs.Text = sdr["Credit"].ToString();
                                }
                            }
                        }
                        conn.Close();
                    }
                }
            }

    Here's result of my demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2019 6:38 AM

All replies

  • User-719153870 posted

    Hi gordon1221,

    DataTextField="Name" DataValueField="Subject"

    You bind text and value both in your two dropdownlists, is it possible that what you actually want to add as value in "GetCourseHrs" is the text not the value?

    If so, you can try change below:

    string SubjectValue = ddlgetSubject.SelectedValue;
    string CourseValue = ddlgetClass.SelectedValue;

    to:

    string SubjectValue = ddlgetSubject.SelectedItem.Text;
    string CourseValue = ddlgetClass.SelectedItem.Text;

    If i'm guessing wrong, maybe you can share us the procedure--"GetCourseHrs".

    Best Regard,

    Yang Shen

    Friday, August 2, 2019 2:10 AM
  • User444756084 posted

    Thanks for the input. Below is the stored procedure. I am getting the correct parameters sent to the stored procedure. 

    Stored Procedure:

    ALTER PROCEDURE [dbo].[GetCourseHrs]
    	-- Add the parameters for the stored procedure here
    	@Subject As varchar(8),
    	@CLASS_NBR As varchar(10)
    
    AS
    BEGIN
    	
    	SET NOCOUNT ON;
    
        -- Insert statements for procedure here
    	SELECT Top 1 ID, Credit
    	FROM [RC_COURSE_TBL] 
    	WHERE (SUBJECT = @Subject)  and CLASS_NBR = @CLASS_NBR
    	
    END

    Friday, August 2, 2019 1:05 PM
  • User-719153870 posted

    Hi gordon1221,

    As you said, your stored procedure is absolutely correct.

    But i found that you can't actually get the HrsTextBox with: TextBox hrs = ListView3.FindControl("hrs") as TextBox;

    Cause there's no textbox whose id is hrs in your listview3.

    Please refer to below code to modify yours:

    protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList ddlClass = (DropDownList)sender;
                ListViewItem item1 = (ListViewItem)ddlClass.NamingContainer;
                DropDownList ddlgetSubject = (DropDownList)item1.FindControl("ddlSubject");
                DropDownList ddlgetClass = (DropDownList)item1.FindControl("ddlClass");
                TextBox hrs = (TextBox)item1.FindControl("HrsTextBox");
    
                string SubjectValue = ddlgetSubject.SelectedValue;
                string CourseValue = ddlgetClass.SelectedValue;
    
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["DConString"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "GetCourseHrs";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Subject", SubjectValue.ToString());
                        cmd.Parameters.AddWithValue("@CLASS_NBR", CourseValue.ToString());
                        
                        cmd.Connection = conn;
                        conn.Open();
    
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            while (sdr.Read())
                            {
                                if (sdr.HasRows)
                                {
                                    hrs.Text = sdr["Credit"].ToString();
                                }
                            }
                        }
                        conn.Close();
                    }
                }
            }

    Here's result of my demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2019 6:38 AM
  • User444756084 posted

    Thank you! This works!!

    Monday, August 5, 2019 3:04 PM