ASP.NET Formview not correctly Updating - c#

I'm converting my website from Classic ASP to ASP.net and outside of a few bumps, I've had no problems until today. But I am learning on the fly.
I'm trying to update a record in a formview and every time I do, I get the following error: "Must declare the scalar variable "#FirstName"".
I've googled and searched, and can't see what I'm doing wrong. I'm sure there's something little that I'm doing incorrect.
<asp:FormView ID="formRecruit" runat="server" DataSourceID="sourceRecruit"
EnableModelValidation="True" DataKeyNames="ID">
<ItemTemplate>
<asp:Table ID="Table1" Width="100%" runat="server">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell Width="50%">
<asp:Table ID="Table2" Width="100%" HorizontalAlign="Center" runat="server">
<asp:TableRow ID="TableRow2" runat="server">
<asp:TableCell>
<asp:Table ID="Table3" Width="100%" runat="server">
<asp:TableRow ID="TableRow3" runat="server">
<asp:TableCell>Name:</asp:TableCell>
<asp:TableCell><%#Eval("FirstName") %></asp:TableCell>
<asp:TableCell><asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="LinkButton1" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</ItemTemplate>
<EditItemTemplate>
<asp:Table ID="Table1" Width="100%" runat="server">
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell Width="50%">
<asp:Table ID="Table2" Width="100%" HorizontalAlign="Center" runat="server">
<asp:TableRow ID="TableRow2" runat="server">
<asp:TableCell>
<asp:Table ID="Table3" Width="100%" runat="server">
<asp:TableRow ID="TableRow3" runat="server">
<asp:TableCell>Name:</asp:TableCell>
<asp:TableCell><asp:TextBox ID="txtFirstName" runat="server" Text='<%#Bind("FirstName") %>'/></asp:TableCell>
<asp:TableCell><asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="LinkButton1" /><asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="LinkButton2" CausesValidation="false" /></asp:TableCell>
</asp:TableRow>
</asp:Table>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sourceRecruit" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT * FROM [Players] WHERE ([ID] = #ID)"
UpdateCommand="UPDATE [Players] SET [FirstName] = #FirstName WHERE [ID] = #ID">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

Not 100% sure, but try to have the SqlDataSource this way and see if it works:
<asp:SqlDataSource ID="sourceRecruit" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:DBCS %>"
SelectCommand="SELECT * FROM [Players] WHERE ([ID] = #ID)"
UpdateCommand="UPDATE [Players] SET [FirstName] = #FirstName WHERE [ID] = #ID">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="ID" Name="ID" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
You may also want to try converting the asp:Table, asp:TableRow tags etc to their HTML equivalent. It's been quite some time since I last worked with ASP.NET, but if I remember right, the controls somehow does not play well with data sources when you put them inside some other server controls.

Related

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();
}
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" />

how to show data from db in dataList view on the basis of dropdown-list items

I want to show my data in data list view on the basis of selected item from drop down menu..
I have bonded the data with my data list view its showing all the data columns except the image ..
How should i display my image data from db as well .. on the basis of selected item from drop down it should respond but its not showing any response..
I didn't use any code behind logic for this all.. i have done it by the help of wizard to generate queries and bind specific data with my db, and show data to its respective controls..
if someone has done related work .. please comment out
thanks..
This is my code so far
<strong><span class="auto-style2">I wanna see: </span></strong> <strong><asp:DropDownList ID="DropDownList1" runat="server" Height="44px" Width="214px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="auto-style3" DataTextField="Brand" DataValueField="Brand">
<asp:ListItem Selected="True">clothes</asp:ListItem>
<asp:ListItem>jewelry</asp:ListItem>
<asp:ListItem>shoes</asp:ListItem>
</asp:DropDownList>
</strong>
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
Width="99%" BorderColor="Blue" BorderStyle="Solid" BorderWidth="3px" DataSourceID="SqlDataSource1" Height="456px">
<ItemTemplate>
Brand:
<asp:Label ID="BrandLabel" runat="server" Text='<%# Eval("Brand") %>'></asp:Label>
<br />
<br />
Image:<br />
<br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Image") %>' />
<br />
<br />
Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
<br />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
<br />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:loginString %>" SelectCommand="SELECT [Brand], [Price], [Image], [Description] FROM [w_product] WHERE ([Category] = #Category)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue="clothes" Name="Category" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

Could not find Control in ControlParameter - prepending with ContentPlaceHolderID not working

I'm having the classic "EditItemTemplate"/"Cannot find Control" error, because I have search parameters that refer to a ControlID inside an EditItemTemplate inside a GridView. I've tried prepending my ControlID with the ID of the GridView (gridview$controlID), but it isn't working. I'm happy to resort to using "code-behind" methods if I have to, but I'd much rather understand why the prepending method isn't working. Any tips based on the code below?
Thanks!
<asp:UpdatePanel ID="upMatchEntry" ChildrenAsTriggers="false" EnableViewState="false" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlGameWeek" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:GridView ID="gvMatchSelect" DataKeyNames="MatchID,HomeTeamID,AwayTeamID"
DataSourceID="sdsMatches" runat="server" AutoGenerateEditButton="True"
AutoGenerateColumns="False" EmptyDataText="No Matches Found" ContentPlaceHolderID="cpMatchSelect">
<Columns>
<asp:TemplateField HeaderText="Home Team" SortExpression="HomeTeamName" ItemStyle-HorizontalAlign="left">
<ItemTemplate><%# Eval("HomeTeamName") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlHomeTeam" DataSourceID="sdsHomeTeams"
DataTextField="HomeTeamName" DataValueField="HomeTeamID"
SelectedValue='<%# Bind("HomeTeamID") %>' AutoPostBack="True" />
</EditItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Away Team" SortExpression="AwayTeamName" ItemStyle-HorizontalAlign="left">
<ItemTemplate><%# Eval("AwayTeamName") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlAwayTeam" DataSourceID="sdsAwayTeams"
DataTextField="AwayTeamName" DataValueField="AwayTeamID"
SelectedValue='<%# Bind("AwayTeamID") %>' AutoPostBack="True"/>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdsMatches" ConnectionString='<%$ ConnectionStrings:ApplicationServices %>' runat="server"
SelectCommand="pGameWeekMatchAdminDisplay" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="GameWeekID" ControlID="ddlGameWeek" PropertyName="SelectedValue" Type="Int32" DefaultValue="-1" />
<asp:ControlParameter Name="DivisionID" ControlID="ddlDivision" PropertyName="SelectedValue" Type="Int32" DefaultValue="-1" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsHomeTeams" ConnectionString='<%$ ConnectionStrings:ApplicationServices %>' runat="server"
SelectCommand="SELECT 'Select Home Team' AS HomeTeamName, -1 AS HomeTeamID UNION SELECT Name AS HomeTeamName, TeamID AS HomeTeamID FROM Team WHERE DivisionID = #DivisionID AND TeamID <> #AwayTeamID" >
<SelectParameters>
<asp:ControlParameter Name="DivisionID" ControlID="ddlDivision" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter Name="AwayTeamID" ControlID="gvMatchSelect$ddlAwayTeam" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsAwayTeams" ConnectionString='<%$ ConnectionStrings:ApplicationServices %>' runat="server"
SelectCommand="SELECT 'Select Away Team' AS AwayTeamName, -1 AS AwayTeamID UNION SELECT Name AS AwayTeamName, TeamID AS AwayTeamID FROM Team WHERE DivisionID = #DivisionID AND TeamID <> #HomeTeamID" >
<SelectParameters>
<asp:ControlParameter Name="DivisionID" ControlID="ddlDivision" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter Name="HomeTeamID" ControlID="gvMatchSelect$ddlHomeTeam" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
The controls are nested inside of a GridViewRow which implements the INamingContainer interface. If you're using a recent version of ASP.NET and only need single-row editing, you should be able to work around this by making the control ID static:
ClientIDMode="Static"

Insert database row via ASP.net 4.5 GridView on-board functionality?

I would like to insert a database entry by using standard functions of my GridView and SqlDataSource. Has there been any update on this topic since the dirty workarounds some versions ago (e.g. see here, here)? I could not find any new tutorials on the web.
For example, is there any handler that allows the entry to be written to the database by using the standard -Datafield and the UpdateCommand of the SqlDataSource? At the moment, the UpdateCommand does not seem to get my entered value.
How can I debug those SqlDataSource-Database-Queries properly?
Heres the solution I've tried to get it working with: My database table tblname contains only one column, "name".
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VConStr %>"
DeleteCommand="DELETE FROM [tblname] WHERE [name] = #name"
InsertCommand="INSERT INTO [tblname] ([name]) VALUES (#name)"
SelectCommand="SELECT [name] FROM [tblname]"
UpdateCommand="UPDATE [tblname] SET [name] = #name WHERE [name] = #name">
<DeleteParameters><asp:Parameter Name="name" Type="String" /></DeleteParameters>
<InsertParameters><asp:Parameter Name="name" Type="String" /></InsertParameters>
<UpdateParameters><asp:Parameter Name="name" Type="String" /></UpdateParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="name" DataSourceID="SqlDataSource1" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Name" ShowHeader="True">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="Insertname" name="name_new" runat="server" Text='<%# Bind("name") %>' />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="True">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Edit" />
<asp:Button ID="Button4" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Sure?');" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Button2" runat="server" Text="Edit entry" CommandName="Update" />
<asp:Button ID="Button3" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Button5" runat="server" Text="New" CommandName="Insert" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Insert using a submit button in ASP.NET doesn't work?

Hello I have a bit of ASP.Net Code below. I am trying to figure out how to make my submit button:
<asp:Button ID="makepost" runat="server" Text="Post My Tip/Story"
ValidationGroup="createpost" />
work properly. When I try to add AccessDataSource1.Insert() I get an error saying this:
CS1061: 'ASP.main_aspx' does not contain a definition for
'AccessDataSource1' and no extension method 'AccessDataSource1'
accepting a first argument of type 'ASP.main_aspx' could be found (are
you missing a using directive or an assembly reference?)
What do I need to type to make this work? All of my code is below.
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Main.aspx.cs" Inherits="Main" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<asp:LoginStatus ID="LoginStatus1" runat="server" />
<div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #FFFFFF">
<p style="font-size: 20px">Welcome Back!<br />
Begin Sharing and Reading Space Tips Below!
</p>
<p> Current Posts:
<asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" DataKeyNames="ID">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:BoundField DataField="User" HeaderText="User" SortExpression="User" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Timeof" HeaderText="Timeof"
SortExpression="Timeof" />
<asp:BoundField DataField="Post" HeaderText="Post" SortExpression="Post" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
<asp:CommandField ButtonType="Button" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Posts.mdb"
SelectCommand="SELECT * FROM [UserPost] ORDER BY [Timeof]"
DeleteCommand="DELETE FROM [UserPost] WHERE [ID] = ?"
InsertCommand="INSERT INTO [UserPost] ([ID], [User], [Title], [Timeof], [Post]) VALUES (?, ?, ?, ?, ?)"
UpdateCommand="UPDATE [UserPost] SET [User] = ?, [Title] = ?, [Timeof] = ?, [Post] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="User" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Timeof" Type="DateTime" />
<asp:Parameter Name="Post" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:ControlParameter Name="ID" Type="Int32" ControlID="TextBox5" PropertyName="Text" />
<asp:ControlParameter Name="User" Type="String" ControlID="TextBox1" PropertyName="Text" />
<asp:ControlParameter Name="Title" Type="String" ControlID="TextBox2" PropertyName="Text" />
<asp:ControlParameter Name="Timeof" Type="DateTime" ControlID="TextBox3" PropertyName="Text" ConvertEmptyStringToNull="true"/>
<asp:ControlParameter Name="Post" Type="String" ControlID="TextBox4" PropertyName="Text" />
</InsertParameters>
</asp:AccessDataSource>
</p>
<table><tr><td> <asp:Label ID="Label5" runat="server" Text="ID: "></asp:Label></td><td>
<asp:TextBox ID="TextBox5" runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td> <asp:Label ID="Label1" runat="server" Text="User: "></asp:Label></td><td>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="Label2" runat="server" Text="Title: "></asp:Label></td><td><asp:TextBox ID="TextBox2"
runat="server" ValidationGroup="createpost"></asp:TextBox></td></tr>
<tr><td><asp:Label ID="Label3" runat="server" Text="Date: "></asp:Label></td><td> <asp:TextBox ID="TextBox3"
runat="server"></asp:TextBox></td></tr>
<tr><td valign="top"><asp:Label ID="Label4" runat="server" Text="Story: "></asp:Label></td><td valign="top">
<asp:TextBox ID="TextBox4" runat="server" Height="129px" Width="474px" TextMode="MultiLine"></asp:TextBox></td></tr>
</table>
<asp:Button ID="makepost" runat="server" Text="Post My Tip/Story"
ValidationGroup="createpost" /><br />
</div>
</LoggedInTemplate><AnonymousTemplate>
<asp:Login ID="Login1" runat="server" ForeColor="White">
</asp:Login><br /><div style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; color:#FFFFFF">Please Register to be begin viewing and sharing your
<br />Star Wars Old Republic Tips and Stories!</div>
</AnonymousTemplate>
</asp:LoginView>
</asp:Content>
Your AccessDataSource is inside LoginView, you have to find the control:
Dim AccessDataSource1 As AccessDataSource = DirectCast(LoginView1.FindControl("AccessDataSource1"), AccessDataSource)
Regards.
in C# it would be this then
AccessDataSource AccessDataSource1 = (AccessDataSource)LoginView1.FindControl("AccessDataSource1");
'ASP.main_aspx' does not contain a definition for 'AccessDataSource1'
I think that your AccessDataSource control is located in the wrong place: you've put it in the LoginView. The error message says that the runtime can't find the control in the root of your page. So, try to put the AccessDataSource outside the LoginView.
This line. Take a look at:
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
Do you see AccessDataSource1? Are you perhaps pointing to the incorrect Datasource? Or, did you name it differently and not realize it?

Categories

Resources