DetailsView Error: “Must declare the scalar value…”

I am attempting to use DetailsView in ASP.NET and c# to show a list of students that are associated with the logged-in user (which will be a teacher after they login to the system). I have been trying to using scalar values so my program can be more dynamic once a teacher types in the last name of the student they want to find in the system. I’m not sure why I am getting the error
must declare the scalar value
when I (think I) am declaring it on the ASP side of my code. I’ve been searching for solutions to this for a couple of hours now so any and all help would be greatly appreciated.

ASP.NET Code:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <asp:Table runat="server">
        <asp:TableRow>
            <asp:TableCell>
            </asp:TableCell>
        </asp:TableRow>

        <asp:TableRow>
            <asp:TableCell>
                <asp:Label ID="lblUserBlank" runat="server" Text="joeB" ></asp:Label>
                
                <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
                <asp:Button ID="btSearch" runat="server" OnClick="btSearch_Click" Text="Search" />
                <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="LastName"
                    DataSourceID="SqlDataSource1" Height="50px" Width="125px">
                    <Fields>
                        <asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True" SortExpression="StudentID" />
                        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                        <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                        <asp:BoundField DataField="TShirt_Size" HeaderText="TShirt_Size" SortExpression="TShirt_Size" />
                    </Fields>
                    <EmptyDataTemplate>
                        No Student found
                    </EmptyDataTemplate>
                </asp:DetailsView>
                <asp:SqlDataSource
                    runat="server"
                    ID="SqlDataSource1"
                    ConnectionString="<%$ ConnectionStrings:Lab3 %>"
                    SelectCommand="SELECT Student.StudentID, Student.LastName, Student.FirstName, Student.Age, Student.TShirt_Size FROM Student INNER JOIN Teacher ON Student.TeacherID = Teacher.TeacherID WHERE Teacher.Username = @Username AND Student.LastName = @LastName">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="txtSearch" Name="@LastName" PropertyName="Text" Type="String" />
                        <asp:ControlParameter ControlID="lblUserBlank" Name="@Username" PropertyName="Text" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </asp:TableCell>
        </asp:TableRow>

    </asp:Table>

</asp:Content>

c# code behind:
 protected void btSearch_Click(object sender, EventArgs e)
        {
            DetailsView1.DataBind();
        }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Change this line:

<asp:ControlParameter ControlID="txtSearch" Name="@LastName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="lblUserBlank" Name="@Username" PropertyName="Text" Type="String" />

for this (remove the @ char from the Name flag value):
<asp:ControlParameter ControlID="txtSearch" Name="LastName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="lblUserBlank" Name="Username" PropertyName="Text" Type="String" />


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x