I have a problem about the updating a column of my gridview; when I try to update the field I get this error:
Incorrect syntax near 'nvarchar'.
Must declare the scalar variable "#pid".
Here is a piece of my code:
<asp:GridView ID="GridView1" runat="server" name="gd" Height="100px"
Width="435px" AutoGenerateColumns="False" DataKeyNames="pid" DataSourceID="SqlDataSource1" >
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="pid" HeaderText="pid"
SortExpression="pid" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="hoghoghe rozane" HeaderText="hoghoghe rozane"
SortExpression="hoghoghe rozane" />
<asp:BoundField DataField="paye" HeaderText="paye"
SortExpression="paye" />
<\Columns>
<asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
UpdateCommand="UPDATE [post] SET [name] = #name, hoghoghe_rozane = #hoghoghe_rozane, [paye] = #paye WHERE (pid = #pid)" >
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="hoghoghe_rozane" Type="Int32" />
<asp:Parameter Name="paye" Type="Int32" />
<asp:Parameter Name="pid" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
chek this out : same problem
it says you must change [hoghoghe rozane] into [hoghoghe_rozane]
Update :::
1.Make sure that the type of pid field in database
2.fields should not have spaces in your tables
3.alias should not have spaces too
try this code :
<asp:GridView ID="GridView1" runat="server" name="gd" Height="100px"
Width="435px" AutoGenerateColumns="False" DataKeyNames="pid" DataSourceID="SqlDataSource1" >
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="pid" HeaderText="pid"
SortExpression="pid" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="hoghoghe_rozane" HeaderText="hoghogheRozane"
SortExpression="hoghoghe_rozane" />
<asp:BoundField DataField="paye" HeaderText="paye"
SortExpression="paye" />
<\Columns>
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
UpdateCommand="UPDATE [post] SET [name] = #name, hoghoghe_rozane = #hoghoghe_rozane, [paye] = #paye WHERE (pid = #pid)" >
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="hoghoghe_rozane" Type="Int32" />
<asp:Parameter Name="paye" Type="Int32" />
<asp:Parameter Name="pid" Type="String" />
</UpdateParameters>
hope that would work.
Related
Hello I am working on a project for school. I am building a page that has a DropDownList that is populated through a SQLDataSource. It displays the names of classes from a table in my database called 'COURSES', when one of the classes is selected a GridView is supposed to display data from tables 'STUDENTS' and 'GRADES'. When I make a selection the GridView does not display at all. I think the problem is in my WHERE clause of the SELECT command. Any help would be greatly appreciated!
Here is my aspx file:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ClassStudentEdit.aspx.cs" Inherits="ClassStudentEdit" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Students</title>
<link type="text/css" rel="stylesheet" href="termproject.css" />
</head>
<body>
<form id="form1" runat="server">
<div id="main">
Course Name: <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="CourseName" DataValueField="CourseID"></asp:DropDownList>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PayablesConnectionString %>" SelectCommand="SELECT [CourseName], [CourseID] FROM [COURSES] ORDER BY [CourseName]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" DataKeyNames="CourseID" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:CommandField ButtonType="Button" ShowSelectButton="True" />
<asp:BoundField DataField="StudentID" HeaderText="StudentID" SortExpression="StudentID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:PayablesConnectionString %>" SelectCommand="SELECT COURSES.CourseID, GRADES.Grade, STUDENTS.StudentID, STUDENTS.Title, STUDENTS.FirstName, STUDENTS.LastName FROM COURSES INNER JOIN GRADES ON COURSES.CourseID = GRADES.CourseID INNER JOIN STUDENTS ON GRADES.StudentID = STUDENTS.StudentID WHERE (COURSES.CourseName = #CourseID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CourseID" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="StudentID" DataSourceID="SqlDataSource3">
<Fields>
<asp:BoundField DataField="StudentID" HeaderText="StudentID" InsertVisible="False" ReadOnly="True" SortExpression="StudentID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:PayablesConnectionString %>" DeleteCommand="DELETE FROM [STUDENTS] WHERE [StudentID] = #original_StudentID AND (([Title] = #original_Title) OR ([Title] IS NULL AND #original_Title IS NULL)) AND (([FirstName] = #original_FirstName) OR ([FirstName] IS NULL AND #original_FirstName IS NULL)) AND (([LastName] = #original_LastName) OR ([LastName] IS NULL AND #original_LastName IS NULL)) AND (([Address] = #original_Address) OR ([Address] IS NULL AND #original_Address IS NULL))" InsertCommand="INSERT INTO [STUDENTS] ([Title], [FirstName], [LastName], [Address]) VALUES (#Title, #FirstName, #LastName, #Address)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [STUDENTS] WHERE ([StudentID] = #StudentID)" UpdateCommand="UPDATE [STUDENTS] SET [Title] = #Title, [FirstName] = #FirstName, [LastName] = #LastName, [Address] = #Address WHERE [StudentID] = #original_StudentID AND (([Title] = #original_Title) OR ([Title] IS NULL AND #original_Title IS NULL)) AND (([FirstName] = #original_FirstName) OR ([FirstName] IS NULL AND #original_FirstName IS NULL)) AND (([LastName] = #original_LastName) OR ([LastName] IS NULL AND #original_LastName IS NULL)) AND (([Address] = #original_Address) OR ([Address] IS NULL AND #original_Address IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_StudentID" Type="Int32" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="StudentID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="original_StudentID" Type="Int32" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_FirstName" Type="String" />
<asp:Parameter Name="original_LastName" Type="String" />
<asp:Parameter Name="original_Address" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<br />
</div>
</form>
</body>
</html>
This is your sql:
SELECT Courses.Courseid,
Grades.Grade,
Students.Studentid,
Students.Title,
Students.Firstname,
Students.Lastname
FROM Courses
INNER JOIN Grades
ON Courses.Courseid = Grades.Courseid
INNER JOIN Students
ON Grades.Studentid = Students.Studentid
WHERE ( Courses.Coursename = #CourseID )
Isn't it Courses.Courseid instead of Courses.Coursename? So:
WHERE ( Courses.Courseid = #CourseID )
( one of the reasons why i avoid declarative datasource controls )
Please forgive my ignorance. I have a done a bit of web programming but am new to ASP.NET and find it mystifying when trying to do something not out of the box.
I have a GridView that gets its data from a SQL Server view. I have a DetailsView that needs to manipulate data in a table. I cannot figure out how to link the GridView to the DetailsView correctly.
Below is the code for the section of the aspx page that contains the GridView and DetailsView. The latest error in an endless procession of errors is: Must declare the scalar variable "#ID".
I would appreciate any help that anyone can offer. Thanks.
<asp:GridView ID="gvVendorContacts" runat="server" AutoGenerateColumns="False"
DataSourceID="dsApplications_Contacts" AllowSorting="True" CellPadding="4" CellSpacing="2" AutoGenerateSelectButton="True" DataKeyNames="ContactID"
onselectedindexchanged="gvVendorContacts_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#E8F3FF" />
<Columns>
<asp:BoundField DataField="FullName" HeaderText="Name" ReadOnly="True"
SortExpression="FullName" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" />
<asp:BoundField DataField="Office Phone" HeaderText="Office Phone"
SortExpression="Office Phone" />
<asp:BoundField DataField="Mobile Phone" HeaderText="Mobile Phone"
SortExpression="Mobile Phone" />
<asp:TemplateField HeaderText="Email" SortExpression="Email">
<ItemTemplate>
<%#Eval("Email")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="dsApplications_Contacts" runat="server"
ConnectionString="<%$ ConnectionStrings:MDSConnectionString %>"
SelectCommand="SELECT [ApplicationID], [ContactID], [FName], [LName], [FullName], [Title], [Role], [OfficePhone] As [Office Phone], [MobilePhone] As [Mobile Phone], [Email] FROM [vw_Applications_Contacts] WHERE ApplicationName = 'Bloomberg'">
</asp:SqlDataSource>
<asp:DetailsView ID="dvVendorContacts" runat="server" AutoGenerateRows="False"
DataKeyNames="ID" DataSourceID="dsVendorContacts" Height="100px"
Width="200px" BackColor="White" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" CellPadding="4">
<EditRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<Fields>
<asp:BoundField DataField="ContactID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="FName" HeaderText="FName" SortExpression="FName" />
<asp:BoundField DataField="LName" HeaderText="LName" SortExpression="LName" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Role" HeaderText="Role" SortExpression="Role" />
<asp:BoundField DataField="OfficePhone" HeaderText="OfficePhone"
SortExpression="OfficePhone" />
<asp:BoundField DataField="MobilePhone" HeaderText="MobilePhone"
SortExpression="MobilePhone" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
</Fields>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
</asp:DetailsView>
<asp:SqlDataSource ID="dsVendorContacts" runat="server"
ConnectionString="<%$ ConnectionStrings:MDSConnectionString %>"
DeleteCommand="DELETE FROM [Contacts] WHERE [ID] = #ID"
InsertCommand="INSERT INTO [Contacts] ([FName], [LName], [Title], [Role], [OfficePhone], [MobilePhone], [Email]) VALUES (#FName, #LName, #Title, #Role, #OfficePhone, #MobilePhone, #Email)"
SelectCommand="SELECT [ID], [FName], [LName], [Title], [Role], [OfficePhone], [MobilePhone], [Email] FROM [Contacts] WHERE ([ID] = #ID)"
UpdateCommand="UPDATE [Contacts] SET [FName] = #FName, [LName] = #LName, [Title] = #Title, [Role] = #Role, [OfficePhone] = #OfficePhone, [MobilePhone] = #MobilePhone, [Email] = #Email WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="FName" Type="String" />
<asp:Parameter Name="LName" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Role" Type="String" />
<asp:Parameter Name="OfficePhone" Type="String" />
<asp:Parameter Name="MobilePhone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="FName" Type="String" />
<asp:Parameter Name="LName" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Role" Type="String" />
<asp:Parameter Name="OfficePhone" Type="String" />
<asp:Parameter Name="MobilePhone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="gvVendorContacts" DefaultValue="NULL"
Name="ContactID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Looks like your select statement is expecting #ID but your SelectParameter is ContactId.
I have grid view with folowing complete code With Msaccess DB.I have used the decode function in sql query.When i press the edit button on grid view it shows folowing error
"DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"
My SQL Data source code is below
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DataConnectionString2 %>"
DeleteCommand="DELETE FROM [EMAILS] WHERE [ID] = ?" InsertCommand="INSERT INTO [EMAILS] ([Email], [FULLNAME],[FLAG]) VALUES (?, ?, ?)"
ProviderName="<%$ ConnectionStrings:DataConnectionString2.ProviderName %>" SelectCommand="SELECT ID, Email, FULLNAME, switch(FLAG = 1, 'Allowed', Flag = 0, 'Not Allowed') AS FLAG FROM EMAILS"
UpdateCommand="UPDATE [EMAILS] SET [Email] = ?, [FULLNAME] = ?,[FLAG] = ? WHERE [ID] = ?">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="FULLNAME" Type="String" />
<asp:Parameter Name="FLAG" Type="Int32" />
<asp:Parameter Name="ID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="FULLNAME" Type="String" />
<asp:Parameter Name="FLAG" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
And Grid View Code is bellow
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1"
HorizontalAlign="Center" Width="821px" EmptyDataText="No Emails Found">
<RowStyle BackColor="White" Font-Names="Arial" Font-Size="Small" ForeColor="Black" />
<Columns>
<asp:CommandField ButtonType="Button" ShowEditButton="True" ShowSelectButton="True" ShowDeleteButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" Visible="False" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="FULLNAME" HeaderText="Full Name" SortExpression="FULLNAME" />
<asp:TemplateField HeaderText="Flag" SortExpression="FLAG">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue="<%# Bind('FLAG') %>" AppendDataBoundItems="True">
<asp:ListItem Value="1">Allowed</asp:ListItem>
<asp:ListItem Value="0">Not Allowed</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FLAG") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" Font-Names="Arial" Font-Size="Small"
ForeColor="#FFFFCC" HorizontalAlign="Center" />
<EmptyDataRowStyle BackColor="#804000" Font-Names="Arial" Font-Size="Small" ForeColor="White" />
</asp:GridView>
Please tell me what possible error is here
Check weather the value in Field flag of the row you are trying to edit, is available in the DropDownList in EditItemTemplate
And try removing AppendDataBoundItems="True" in DropDownList in EditItemTemplate
I'm having a wired problem with Radgrid.. Im using a Radgrid to display some results where the user can edit/update/delete them. I'm using a single SQL Server 2000 table to fetch the results... Not sure why the events are not firing in radgrid. But I am able to do it successfully using the Gridview control...
Im using .NET framework 4
Radgrid code
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteColumn="True"
AutoGenerateEditColumn="True" DataSourceID="SSDS" GridLines="None" Width="844px"
DataMember="DefaultView">
<MasterTableView DataSourceID="SSDS" DataKeyNames="id">
<Columns>
<telerik:GridBoundColumn DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id"
UniqueName="id" Visible="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="name" HeaderText="name" SortExpression="name"
UniqueName="name">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="address" HeaderText="address" SortExpression="address"
UniqueName="address">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<br />
<br />
<asp:SqlDataSource ID="SSDS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringSQL %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringSQL.ProviderName %>" SelectCommand="SELECT [id], [name], [address] FROM [sitelinks]"
UpdateCommand="UPDATE [sitelinks] set [name] = ? , [address] = ? where [id] = ?"
DeleteCommand="delete from sitelinks where id = ?">
<DeleteParameters>
<asp:Parameter Name="id" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="name" />
<asp:Parameter Name="address" />
<asp:Parameter Name="id" />
</UpdateParameters>
</asp:SqlDataSource>
Gridview control code (the one that works)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SSDS"
DataKeyNames="id">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" Visible="false" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SSDS" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringSQL %>"
DeleteCommand="delete from sitelinks where id = ?" ProviderName="<%$ ConnectionStrings:ConnectionStringSQL.ProviderName %>"
SelectCommand="select id,name,address from sitelinks" UpdateCommand="update sitelinks set name=?, address=? where id = ? ">
<UpdateParameters>
<asp:ControlParameter ControlID="GridView1" Name="name" />
<asp:ControlParameter ControlID="GridView1" Name="address" />
<asp:ControlParameter ControlID="GridView1" Name="id" />
</UpdateParameters>
<DeleteParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" />
</DeleteParameters>
</asp:SqlDataSource>
Thanks
Set the AllowAutomaticUpdates/AllowAutomaticDeletes properties of the telerik grid to true and see this sample.
I have a gridview that, when in edit mode, I use a dropdown to load all provinces, however, when I change the province and click update, my page crashes...I'm not sending the province parameter to the gridview's updateparameters properly...here's my code, someone please help..
<asp:GridView runat="server" ID="gvUsers" DataKeyNames="UserID" BackColor="#eeeeee" Width="85%"
HorizontalAlign="Center"
Font-Bold="False" Font-Names="Verdana"
Font-Size="10pt" AutoGenerateColumns="False"
OnRowDataBound="gvUsers_RowDataBound"
OnRowDeleting="gvUsers_RowDeleting" >
<HeaderStyle BackColor="#904601" ForeColor="White"
Font-Bold="True" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="Yellow" />
<AlternatingRowStyle BackColor="White" Font-Bold="false" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"
CommandArgument='<%# Eval("UserID") %>'
CommandName="Select" runat="server">
Select</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserID" Visible="False" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandArgument='<%# Eval("UserID") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle HorizontalAlign="Left" />
</asp:GridView><br /><br />
<asp:DetailsView runat="server" ID="dvUser" DataSourceID="SqlDataSource1" AutoGenerateRows="False" Width="85%" DataKeyNames="UserID"
HorizontalAlign="Center" AutoGenerateInsertButton="True" AutoGenerateEditButton="True" OnItemInserted="dvUsers_ItemInserted" >
<Fields>
<asp:BoundField DataField="UserID" Visible="False" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="true" SortExpression="UserName" />
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label runat="server" ID="lblPassword" Text="●●●●●●●●●"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password" Text='<%# Bind("Password") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Birthdate" SortExpression="Birthdate">
<EditItemTemplate>
<asp:TextBox runat="server" ID="Birthdate" Text='<%# Bind("Birthdate") %>' ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="Server" ID="lblBirthdate" Text='<%# Bind("Birthdate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="Apt" HeaderText="Apt" SortExpression="Apt" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:TemplateField HeaderText="Province">
<ItemTemplate>
<asp:Label runat="server" ID="lblProvince" Text='<%# Eval("ProvName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddlProvince" DataValueField="ProvinceID" DataSourceID="SqlDataSource2" DataTextField="Province"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
<asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum" SortExpression="PhoneNum" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:CheckBoxField DataField="ynAdminUser" HeaderText="ynAdminUser" SortExpression="ynAdminUser" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2"
runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT ProvinceID, Province FROM tblProvince"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT a.UserID, a.FirstName, a.LastName, a.UserName, a.Password, a.Birthdate, a.Address, a.Apt, a.City, a.Province, b.Province as 'ProvName',
a.PostalCode, a.PhoneNum, a.Email, a.ynAdminUser
FROM tblUser a
INNER JOIN tblProvince B ON A.Province = B.ProvinceID
WHERE (a.UserID = #UserID) AND (a.ynDelete = 0)"
InsertCommand="INSERT INTO tblUser(FirstName, LastName, UserName, Password, Birthdate, Address, Apt, City, Province, PostalCode, Email, PhoneNum, ynAdminUser, ynDelete) VALUES (#FirstName, #LastName, #UserName, #Password, #Birthdate, #Address, #Apt, #City, #Province, #PostalCode, #Email, #PhoneNum, #ynAdminUser, 0)"
UpdateCommand="UPDATE tblUser SET FirstName = #FirstName, LastName = #LastName, Birthdate = #Birthdate, Address = #Address, Apt = #Apt, City = #City, Province = #Province, PostalCode = #PostalCode, PhoneNum = #PhoneNum, Email = #Email, ynAdminUser = #ynAdminUser WHERE (UserID = #UserID)">
<SelectParameters>
<asp:ControlParameter ControlID="gvUsers" Name="UserID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter DbType="DateTime" Name="BirthDate" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Apt" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:ControlParameter Name="Province" ControlID="ddlProvince" PropertyName="SelectedValue" Type="Int32" />
<asp:Parameter Name="PhoneNum" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="ynAdminUser" Type="Boolean" />
<asp:Parameter Name="UserID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter DbType="DateTime" Name="Birthdate" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="Apt" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="Province" Type="Int32" />
<asp:Parameter Name="PostalCode" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="PhoneNum" Type="String" />
<asp:Parameter Name="ynAdminUser" Type="Boolean" />
</InsertParameters>
</asp:SqlDataSource>
You don't need the control binding:
<asp:ControlParameter Name="Province" ControlID="ddlProvince" PropertyName="SelectedValue" Type="Int32" />
Instead, use a regualar parameter like your others and add the following binding to your drop-down list for the edit mode of the gridview:
SelectedValue=<%#Bind("Province")%>
I realize this is a really old question, and I hope you found your answer.
Looking at your markup, though, I want to say your connection strings were never filled out:
runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
I am not the strongest when it comes to ASP.NET databinding in the markup code, but I am pretty sure your connection string needs to point to an actual string name in your web.Config file's ConnectionStrings section:
<connectionStrings>
<clear />
<add name="Mark1" connectionString="Data Source=localhost;Initial Catalog=Table1;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
Use whatever your actual connection string is, though.
Now, your markup should be written as:
runat="server" ConnectionString="<%$ ConnectionStrings:Mark1 %>"
Again, I'm new to this. I found this only while trying to search for ways to solve my own problems.
If this is wrong (possible), someone just let me know what I did wrong and I'll delete it.