DetailsView update - c#

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

Related

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 update/delete crashes

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.

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?

Insert proper phone format

I have a textbox which stores phone number.
<asp:TextBox runat="server" ID="phone" MaxLength="10" Columns="10" Width="90px" />
<asp:MaskedEditExtender ID="phone_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
Enabled="True" TargetControlID="phone" Mask="(999)-999-9999" ClearMaskOnLostFocus="False">
</asp:MaskedEditExtender>
The question is when I insert phone number 9999999999, the number in the database becomes
(999)-9999.
What I want is 9999999999.
The other code is:
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" ConnectionString="<%$ ConnectionStrings:MembershipDB %>"
InsertCommand="CreateAdditionalAccountInfo" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:ControlParameter Name="FirstName" Type="String" ControlID="FirstName" PropertyName="Text" />
<asp:ControlParameter Name="LastName" Type="String" ControlID="LastName" PropertyName="Text" />
<asp:ControlParameter Name="Degree" Type="String" ControlID="Degree" PropertyName="Text" />
<asp:ControlParameter Name="Organization" Type="String" ControlID="Organization"
PropertyName="Text" />
<asp:ControlParameter Name="Phone" Type="String" ControlID="Phone" PropertyName="Text" />
<asp:ControlParameter Name="Ext" Type="String" ControlID="Ext" PropertyName="Text" />
<asp:ControlParameter Name="last4SSN" Type="String" ControlID="TextSSN" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
The stored Procedure query is
INSERT INTO [User_ExtraInfo] ([UserId], [FirstName], [LastName], [Degree], [Organization], [Phone], [Ext], [last4SSN],[Trained]) VALUES (#UserId, #FirstName, #LastName, #Degree, #Organization, #Phone, #Ext
Thanks.
Change the proc to this:
INSERT INTO [User_ExtraInfo] ([UserId], [FirstName], [LastName], [Degree], [Organization], [Phone], [Ext], [last4SSN],[Trained]) VALUES (#UserId
, #FirstName
, #LastName
, #Degree
, #Organization
, REPLACE(REPLACE( REPLACE(#Phone,'(',''),')','') ,'-','') , #Ext )
The reason I suggest this approach is because you are using a SQLDatasource object and I don't think it's possible to instruct the insert parameter -at least not declaratively from the markup- to ignore the mask. Your best bet is probably just to modify the proc.
UPDATE - programmatic approach:
Add a handler for OnInserting to the SqlDataSource:
<asp:SqlDataSource ID="InsertExtraInfo" runat="server" OnInserting="On_Inserting"
Handle the event on code and do the replacement there:
protected void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e) {
e.Command.Parameters["#Phone"].Value=phone.Text.Replace("..."); // etc
}
Note: not tested but it's the idea.

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