GridView update/delete crashes - c#

I have a gridview of my table. I established a AccsessDataSource to my database. The Accsess datasource is configured so the sql scentance is: SELECT * FROM [Users] In the advanced settings I checked "Generate INSERT, UPDATE, and delete statements" I enabled editing and deleting. My source code shows this:
<asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/RunRunDB.mdb"
DeleteCommand="DELETE FROM [Users] WHERE (([username] = ?) OR ([username] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [Users] ([Fname], [Lname], [Email], [Bday], [pswrd], [admin], [username]) VALUES (?, ?, ?, ?, ?, ?, ?)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [Users]"
UpdateCommand="UPDATE [Users] SET [Fname] = ?, [Lname] = ?, [Email] = ?, [Bday] = ?, [pswrd] = ?, [admin] = ? WHERE (([username] = ?) OR ([username] IS NULL AND ? IS NULL))">`
<DeleteParameters>
<asp:Parameter Name="original_username" Type="String" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Fname" Type="String" />
<asp:Parameter Name="Lname" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Bday" Type="DateTime" />
<asp:Parameter Name="pswrd" Type="String" />
<asp:Parameter Name="admin" Type="Boolean" />
<asp:Parameter Name="username" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Fname" Type="String" />
<asp:Parameter Name="Lname" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Bday" Type="DateTime" />
<asp:Parameter Name="pswrd" Type="String" />
<asp:Parameter Name="admin" Type="Boolean" />
<asp:Parameter Name="original_username" Type="String" />
</UpdateParameters>
</asp:AccessDataSource>
Each time i run my website i get an error that says: You do not have a value for one or more of the required parameters.
Thanks..

Your problem is here:
WHERE (([username] = ?) OR ([username] IS NULL AND ? IS NULL))
Command parameters cannot be used to specify table or column (field) names; they only specify column values. You will have to adjust your UpdateCommand and DeleteCommand strings to specify the column name explicitly.

Related

Handling checkbox in <asp:ControlParameter> section of SqlDataSource

I'm reusing some code that I've had a lot of success with on other pages and the only significant difference here is that I'm capturing the value of a Checkbox in the filter panel that I haven't done in the past. Code sample is provided below, but the crux of what I'm running into is that I can't seem to find an acceptable set of parameters for the listed under the within the SqlDataSource.
What it throws is this: "The data types varchar and bit are incompatible in the add operator."
This is when I'm using Type="Boolean" in the asp:ControlParameter. I've tried numerious Types and other syntax within the asp:ControlParameter with no success.
<%--From the filter pannel--%>
<%--The SqlDataSource--%>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" CancelSelectOnNullParameter="false" ConnectionString='<%$ ConnectionStrings:csrConnectionString %>' DeleteCommand="DELETE FROM [csr_refdata_ip360_HostVulnerabilityCSV] WHERE [RecID] = #RecID" InsertCommand="INSERT INTO [csr_refdata_ip360_HostVulnerabilityCSV] ([DNS Name], [NetBIOS Name], [IP], [OS], [Vulnerability Score], [Vulnerability], [Vulnerability ID], [TicketNumber], [TicketClosed], [AssetID], [ExceptionID], [Notes]) VALUES (#DNS_Name, #NetBIOS_Name, #IP, #OS, #Vulnerability_Score, #Vulnerability, #Vulnerability_ID, #TicketNumber, #TicketClosed, #AssetID, #ExceptionID, #Notes)" SelectCommand="SELECT RecID, [DNS Name] AS DNS_Name, [NetBIOS Name] AS NetBIOS_Name, IP, OS, [Vulnerability Score] AS Vulnerability_Score, Vulnerability, [Vulnerability ID] AS Vulnerability_ID, TicketNumber, TicketClosed, AssetID, ExceptionID, Notes FROM csr_refdata_ip360_HostVulnerabilityCSV WHERE (Excepted = #Excepted) AND (RTRIM(LTRIM(ISNULL(#RecID, ''))) = '' OR RecID LIKE '%' + #RecID + '%') AND (RTRIM(LTRIM(ISNULL(#DNS_Name, ''))) = '' OR [DNS Name] LIKE '%' + #DNS_Name + '%') AND (RTRIM(LTRIM(ISNULL(#NetBIOS_Name, ''))) = '' OR [NetBIOS Name] LIKE '%' + #NetBIOS_Name + '%') AND (RTRIM(LTRIM(ISNULL(#IP, ''))) = '' OR IP LIKE '%' + #IP + '%') AND (RTRIM(LTRIM(ISNULL(#OS, ''))) = '' OR OS LIKE '%' + #OS + '%') AND (RTRIM(LTRIM(ISNULL(#Vulnerability_Score, ''))) = '' OR [Vulnerability Score] LIKE '%' + #Vulnerability_Score + '%') AND (RTRIM(LTRIM(ISNULL(#Vulnerability, ''))) = '' OR Vulnerability LIKE '%' + #Vulnerability + '%') AND (RTRIM(LTRIM(ISNULL(#Vulnerability_ID, ''))) = '' OR [Vulnerability ID] LIKE '%' + #Vulnerability_ID + '%') AND (RTRIM(LTRIM(ISNULL(#TicketNumber, ''))) = '' OR TicketNumber LIKE '%' + #TicketNumber + '%') AND (RTRIM(LTRIM(ISNULL(#TicketClosed, ''))) = '' OR TicketClosed LIKE '%' + #TicketClosed + '%') AND (RTRIM(LTRIM(ISNULL(#Notes, ''))) = '' OR Notes LIKE '%' + #Notes + '%') AND (RTRIM(LTRIM(ISNULL(#AssetID, ''))) = '' OR AssetID LIKE '%' + #AssetID + '%') AND (RTRIM(LTRIM(ISNULL(#ExceptionID, ''))) = '' OR ExceptionID LIKE '%' + #ExceptionID + '%') ORDER BY Vulnerability_Score DESC, Vulnerability, NetBIOS_Name" UpdateCommand="UPDATE [csr_refdata_ip360_HostVulnerabilityCSV] SET [DNS Name] = #DNS_Name, [NetBIOS Name] = #NetBIOS_Name, [IP] = #IP, [OS] = #OS, [Vulnerability Score] = #Vulnerability_Score, [Vulnerability] = #Vulnerability, [Vulnerability ID] = #Vulnerability_ID, [TicketNumber] = #TicketNumber, [TicketClosed] = #TicketClosed, [AssetID] = #AssetID, [ExceptionID] = #ExceptionID, [Notes] = #Notes WHERE [RecID] = #RecID">
<DeleteParameters>
<asp:Parameter Name="RecID" Type="Int32"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="DNS_Name" Type="String"></asp:Parameter>
<asp:Parameter Name="NetBIOS_Name" Type="String"></asp:Parameter>
<asp:Parameter Name="IP" Type="String"></asp:Parameter>
<asp:Parameter Name="OS" Type="String"></asp:Parameter>
<asp:Parameter Name="Vulnerability_Score" Type="Int16"></asp:Parameter>
<asp:Parameter Name="Vulnerability" Type="String"></asp:Parameter>
<asp:Parameter Name="Vulnerability_ID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="TicketNumber" Type="Int32"></asp:Parameter>
<asp:Parameter Name="TicketClosed" Type="Boolean"></asp:Parameter>
<asp:Parameter Name="AssetID" Type="Int64"></asp:Parameter>
<asp:Parameter Name="ExceptionID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="Notes" Type="String"></asp:Parameter>
</InsertParameters>
<SelectParameters>
<asp:Parameter DefaultValue="N" Name="Excepted" Type="String"></asp:Parameter>
<asp:ControlParameter Name="RecID" ControlID="RecID_Tbx" Type="Int32" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="DNS_Name" ControlID="DNSName_Tbx" Type="String" ConvertEmptyStringToNull="true"/>
<asp:ControlParameter Name="NetBIOS_Name" ControlID="NetBIOSName_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="IP" ControlID="IP_Tbx" Type="String" ConvertEmptyStringToNull="true"/>
<asp:ControlParameter Name="OS" ControlID="OS_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Vulnerability_Score" ControlID="Vulnerability_Tbx" Type="Int16" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Vulnerability" ControlID="Vulnerability_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Vulnerability_ID" ControlID="VulnerabilityID_Tbx" Type="Int32" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="TicketNumber" ControlID="TicketNum_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<%--<%--<%-- This is the problem line below --%>--%>--%>
<asp:ControlParameter Name="TicketClosed" ControlID="TicketClosed_ChBox" Type="Boolean" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Notes" ControlID="Notes_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetID" ControlID="AssetID_Tbx" Type="Int64" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="ExceptionID" ControlID="ExceptionID_Tbx" Type="String" ConvertEmptyStringToNull="true" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DNS_Name" Type="String"></asp:Parameter>
<asp:Parameter Name="NetBIOS_Name" Type="String"></asp:Parameter>
<asp:Parameter Name="IP" Type="String"></asp:Parameter>
<asp:Parameter Name="OS" Type="String"></asp:Parameter>
<asp:Parameter Name="Vulnerability_Score" Type="Int16"></asp:Parameter>
<asp:Parameter Name="Vulnerability" Type="String"></asp:Parameter>
<asp:Parameter Name="Vulnerability_ID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="TicketNumber" Type="Int32"></asp:Parameter>
<asp:Parameter Name="TicketClosed" Type="Boolean"></asp:Parameter>
<asp:Parameter Name="AssetID" Type="Int64"></asp:Parameter>
<asp:Parameter Name="ExceptionID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="Notes" Type="String"></asp:Parameter>
<asp:Parameter Name="RecID" Type="Int32"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
Any insight on how I can stuff this checkbox into my ControlParameter properly would be greatly appreciated. I've pretty much exhausted my ideas.
I ended up altering the db table changing the column to varchar(1) so that I could set all BoundFields in the gridview. Then updated my to:
<SelectParameters>
<asp:Parameter DefaultValue="N" Name="Excepted" Type="String"></asp:Parameter>
<asp:ControlParameter Name="RecID" ControlID="RecID_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="DNS_Name" ControlID="DNSName_Tbx" Type="String" ConvertEmptyStringToNull="true"/>
<asp:ControlParameter Name="NetBIOS_Name" ControlID="NetBIOSName_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="IP" ControlID="IP_Tbx" Type="String" ConvertEmptyStringToNull="true"/>
<asp:ControlParameter Name="OS" ControlID="OS_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Vulnerability_Score" ControlID="VulnerabilityScore_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Vulnerability" ControlID="Vulnerability_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Vulnerability_ID" ControlID="VulnerabilityID_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="TicketNumber" ControlID="TicketNum_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="TicketClosed" ControlID="TicketClosed_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="Notes" ControlID="Notes_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="AssetID" ControlID="AssetID_Tbx" Type="String" ConvertEmptyStringToNull="true" />
<asp:ControlParameter Name="ExceptionID" ControlID="ExceptionID_Tbx" Type="String" ConvertEmptyStringToNull="true" />
Everything is working fine now....
MethodMan, thanks for your input! This one is done!

Trying to insert data into database with sqldatasource, I get this error: ora-01745 invalid host exception, why?

I have this code in my aspx file:
<asp:SqlDataSource ID="RegisterUserSQL" runat="server" ConnectionString="<%$ ConnectionStrings:UserQueries %>" ProviderName="<%$ ConnectionStrings:UserQueries.ProviderName %>"
InsertCommand="insert into users (firstname, lastname, username, password, email, gender, birthdate, nationality, currentcity)
values (:firstname, :lastname, :username, :password, :email, :gender, :date, :nationality, :currentcity)">
<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 Name="email" Type="String" />
<asp:Parameter Name="gender" Type="Char" />
<asp:Parameter Name="date" Type="String" />
<asp:Parameter Name="nationality" Type="String" />
<asp:Parameter Name="currentcity" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Then this is the code behind:
RegisterUserSQL.InsertParameters["firstname"].DefaultValue = SavedFirstnameBox.Text;
RegisterUserSQL.InsertParameters["lastname"].DefaultValue = SavedLastnameBox.Text;
RegisterUserSQL.InsertParameters["username"].DefaultValue = SavedUsernameBox.Text;
RegisterUserSQL.InsertParameters["password"].DefaultValue = SavedPasswordBox.Text;
RegisterUserSQL.InsertParameters["email"].DefaultValue = SavedEmailBox.Text;
RegisterUserSQL.InsertParameters["gender"].DefaultValue = SavedGenderBox.Text;
RegisterUserSQL.InsertParameters["date"].DefaultValue = BirthdateBox.Text;
RegisterUserSQL.InsertParameters["nationality"].DefaultValue = SavedNationalityBox.Text;
RegisterUserSQL.InsertParameters["currentcity"].DefaultValue = SavedCurrentCityBox.Text;
RegisterUserSQL.Insert();
It stops at the last insert() statement and gives the error.
Any suggestions?

Gridview with access data source will not update or delete asp c#

I've looked all over, studied books, tutorial videos and numerous articles, and I can't solve this problem. Within visual studio 2010 insert a gridview, I specify the data source as a access database. Within the specification I include the option to insert, update and delete records. The gridview appears on the screen, and when I test the webpage it loads up with the relevant buttons for updating and inserting etc.
When testing the page, I click the update button, alter a row and then click update. The page goes back to how it was previously, the record remains the same. I refresh the page to make certain its not updated, and it definitely hasn't.
I've followed tutorials exactly, and can't get the fields to update or delete when using this method. The database im using is an access one, an mdb file.
I'm sure im missing something massively simple here, but im not sure what. If anyone could offer some help I'd be very grateful! thanks
Here is my code for the form.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Book Id" DataSourceID="SqlDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="Book Id" HeaderText="Book Id" InsertVisible="False"
ReadOnly="True" SortExpression="Book Id" />
<asp:BoundField DataField="ISBN Number" HeaderText="ISBN Number"
SortExpression="ISBN Number" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Author" HeaderText="Author"
SortExpression="Author" />
<asp:BoundField DataField="Publisher" HeaderText="Publisher"
SortExpression="Publisher" />
<asp:BoundField DataField="Date Published" HeaderText="Date Published"
SortExpression="Date Published" />
<asp:CheckBoxField DataField="Availability" HeaderText="Availability"
SortExpression="Availability" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:booksellerConnectionString %>"
DeleteCommand="DELETE FROM [tblBooks] WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [tblBooks] ([Book Id], [ISBN Number], [Title], [Author], [Publisher], [Date Published], [Availability], [Price]) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
OldValuesParameterFormatString="original_{0}"
ProviderName="<%$ ConnectionStrings:booksellerConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM [tblBooks]"
UpdateCommand="UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_Book_Id" Type="Int32" />
<asp:Parameter Name="original_ISBN_Number" Type="String" />
<asp:Parameter Name="original_ISBN_Number" Type="String" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_Author" Type="String" />
<asp:Parameter Name="original_Author" Type="String" />
<asp:Parameter Name="original_Publisher" Type="String" />
<asp:Parameter Name="original_Publisher" Type="String" />
<asp:Parameter Name="original_Date_Published" Type="String" />
<asp:Parameter Name="original_Date_Published" Type="String" />
<asp:Parameter Name="original_Availability" Type="Boolean" />
<asp:Parameter Name="original_Price" Type="Decimal" />
<asp:Parameter Name="original_Price" Type="Decimal" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Book_Id" Type="Int32" />
<asp:Parameter Name="ISBN_Number" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Author" Type="String" />
<asp:Parameter Name="Publisher" Type="String" />
<asp:Parameter Name="Date_Published" Type="String" />
<asp:Parameter Name="Availability" Type="Boolean" />
<asp:Parameter Name="Price" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ISBN_Number" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Author" Type="String" />
<asp:Parameter Name="Publisher" Type="String" />
<asp:Parameter Name="Date_Published" Type="String" />
<asp:Parameter Name="Availability" Type="Boolean" />
<asp:Parameter Name="Price" Type="Decimal" />
<asp:Parameter Name="original_Book_Id" Type="Int32" />
<asp:Parameter Name="original_ISBN_Number" Type="String" />
<asp:Parameter Name="original_ISBN_Number" Type="String" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_Title" Type="String" />
<asp:Parameter Name="original_Author" Type="String" />
<asp:Parameter Name="original_Author" Type="String" />
<asp:Parameter Name="original_Publisher" Type="String" />
<asp:Parameter Name="original_Publisher" Type="String" />
<asp:Parameter Name="original_Date_Published" Type="String" />
<asp:Parameter Name="original_Date_Published" Type="String" />
<asp:Parameter Name="original_Availability" Type="Boolean" />
<asp:Parameter Name="original_Price" Type="Decimal" />
<asp:Parameter Name="original_Price" Type="Decimal" />
</UpdateParameters>
</asp:SqlDataSource>
asp.cs file below;
namespace Second
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
I don't necessarily have a straight answer, but some suggestions:
It might be because of the complexity of your UPDATE:
UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ? AND (([ISBN Number] = ?) OR ([ISBN Number] IS NULL AND ? IS NULL)) AND (([Title] = ?) OR ([Title] IS NULL AND ? IS NULL)) AND (([Author] = ?) OR ([Author] IS NULL AND ? IS NULL)) AND (([Publisher] = ?) OR ([Publisher] IS NULL AND ? IS NULL)) AND (([Date Published] = ?) OR ([Date Published] IS NULL AND ? IS NULL)) AND [Availability] = ? AND (([Price] = ?) OR ([Price] IS NULL AND ? IS NULL))
Because you've got quite a hefty WHERE clause. If you just pared it back to:
UPDATE [tblBooks] SET [ISBN Number] = ?, [Title] = ?, [Author] = ?, [Publisher] = ?, [Date Published] = ?, [Availability] = ?, [Price] = ? WHERE [Book Id] = ?
And trimmed back your <UpdateParameters> accordingly, does that have any effect?
Also (and it's been a little while for me) I noticed that your parameter names (e.g. Book_Id) do not match exactly the data field bindings (e.g. Book Id) so that might be causing an issue. I believe that they need to match up (or it is good that they do) for binding purposes but in this case they don't.
It might be worth modifying your database so that the column names are things like [Book_Id], [ISBN_Number] etc...I think it is quite rare to have spaces in database column (certainly rare from my experience...but I am not that experienced!)
Just for kicks, try removing the line
ConflictDetection="CompareAllValues". Will the update occur now?

DetailsView update

I have a GridView that shows all the registed users and some of their information and when you choose the "select" link off the side, it shows a DetailsView with all the details related to that user. The select works fine and updates the details view for the selected user, and when I try to edit the DetailsView, it will update the database and fill in the information updated for ALL the users when changing the fields (eg. if I change the customers first name, everyone registered will get that name). Here's my aspx code for my SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [UserProfile] ORDER BY [Email], [LastName], [Company]"
UpdateCommand="UPDATE [UserProfile] SET [LastName] = #LastName, [PartsList] = #PartsList, [UserName] = #UserName, [Question] = #Question,
[Answer] = #Answer, [Role] = #Role
WHERE [FirstName] = FirstName AND [Company] = Company">
<UpdateParameters>
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="Company" Type="String" />
<asp:Parameter Name="PartsList" Type="String" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="Password" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Question" Type="String" />
<asp:Parameter Name="Answer" Type="String" />
<asp:Parameter Name="Role" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
You forgot #original_ in your Where parameters
Change UpdateCommand to:
UPDATE [UserProfile] SET [LastName] = #LastName, [PartsList] = #PartsList, [UserName] = #UserName, [Question] = #Question,
[Answer] = #Answer, [Role] = #Role
WHERE [FirstName] = #original_FirstName AND [Company] = #original_Company

ASP.NET : SqlDataSource insert - identity parameter

I have an asp.net application in which I am inserting into an sql table using a GridView linked to an SqlDataSource. The problem is that I do not know the 'Identity' parameter to use on insertion. The 'Identity' column should be automatically incrementing, however upon inserting, I get an exception that the Identity param must not be null. How do I either craft the insert statement so that this works or how do I get the next correct identity # to specify? Thanks.
Here is my SqlDataSource code:
<asp:SqlDataSource ID="VehiclesSqlDS" runat="server"
ConnectionString="<%$ ConnectionStrings:RamRideOpsConnectionString %>"
SelectCommand="SELECT * FROM [Vehicles]"
ConflictDetection="CompareAllValues"
InsertCommand="INSERT INTO [Vehicles] ([Identity], [CarNum], [MaxPassengers], [Status], [CurrPassengers], [StartAdd], [EndAdd], [AvgRideTime], [numRides]) VALUES (#Identity, #CarNum, #MaxPassengers, #Status, #CurrPassengers, #StartAdd, #EndAdd, #AvgRideTime, #numRides)"
OnInserting="VehiclesSqlDS_Insert"
OldValuesParameterFormatString="original_{0}">
<InsertParameters>
<asp:Parameter Name="Identity" Type="Int32 "/>
<asp:Parameter Name="CarNum" Type="Int32" DefaultValue="-1"/>
<asp:Parameter Name="MaxPassengers" Type="Int32" DefaultValue="3" />
<asp:Parameter Name="Status" Type="String" DefaultValue="Ready" />
<asp:Parameter Name="CurrPassengers" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="StartAdd" Type="String" />
<asp:Parameter Name="EndAdd" Type="String" />
<asp:Parameter DbType="Time" Name="AvgRideTime" />
<asp:Parameter Name="numRides" Type="Int32" DefaultValue="0" />
</InsertParameters>
</asp:SqlDataSource>
you need to set parameter direction to OUTPut, to get the identity value. from MSDN
and handle the onInserted event to get, the identity value
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
string sID = e.Command.Parameters["#Identity"].Value.ToString();
//Display new ID
}
you can try like this.
<asp:sqlDataSource ID="Datasource"
SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = #EmpID"
InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (#LastName, #FirstName);
SELECT #EmpID = SCOPE_IDENTITY()"
UpdateCommand="UPDATE Employees SET LastName=#LastName, FirstName=#FirstName
WHERE EmployeeID=#EmployeeID"
DeleteCommand="DELETE Employees WHERE EmployeeID=#EmployeeID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
</InsertParameters>
</asp:sqlDataSource>
You should omit the Identity column from the insert statement. Only include the data columns. The Sql Server will create the auto-increment then.
INSERT INTO [Vehicles] ([CarNum], [MaxPassengers], [Status], [CurrPassengers], [StartAdd], [EndAdd], [AvgRideTime], [numRides]) VALUES (#CarNum, #MaxPassengers, #Status, #CurrPassengers, #StartAdd, #EndAdd, #AvgRideTime, #numRides)
Ever Present, that is assuming that Identity is an auto-incremeneted column. It could just be a regular column.
You need to somehow retrieve the latest Identity first before trying to insert.
Alternatively you could write a trigger on the database that automatically updates the Identity column when a new item is added.
These snippets may help:
<InsertParameters>
<asp:Parameter Name="SalesRepID" Type="Int32" />
...more params
<asp:Parameter Name="QuoteID" Type="Int32" Direction="Output" />
</InsertParameters>
Note the second command added
InsertCommand="INSERT INTO [MyTable] (fields); SET #QuoteID = Scope_Identity();"

Categories

Resources