I would like to insert UserName from aspnet_users into my own table called Members. My sqldatasource looks like as follows. At the end in the InsertParameters I have added ProfileParameter but its not working. Just to get the values going, I have hardcoded 'testhere' value in the insertcommand. My SQLDatasource looks as follows.
Please help !!!
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
OnInserted= "on_inserted"
OnUpdated = "on_updated"
ConnectionString="<%$ ConnectionStrings:test1ConnectionString %>"
DeleteCommand="DELETE FROM [member] WHERE [id] = #id"
InsertCommand="INSERT INTO [member] ([firstname], [lastname],[username]) VALUES (#firstname, #lastname,'testhere');
set #id = ##identity"
SelectCommand="SELECT * FROM [member] WHERE ([id] = #id)"
UpdateCommand="UPDATE [member] SET [firstname] = #firstname, [lastname] = #lastname WHERE [id] = #id"
onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="id" Type=Int32 Direction=Output />
<asp:Parameter Name="firstname" Type="String" />
<asp:Parameter Name="lastname" Type="String" />
<asp:ProfileParameter Name="username" Type=String />
</InsertParameters>
</asp:SqlDataSource>
Okay.. as a simple one:
Try changing the InsertParameter (username) to be an asp:Parameter.
Add a asp:HiddenField control with ID of username
onpageload, set the value of the username hiddenfield to be Page.User.Identity.Name
Magic!
Related
I am updating the table in database by entering some fields in same page containing INPUT fields and Grid. but grid which is connected to that table is not refreshing after submit the value. I have to open that page in new tab to view the changes. Also i want those input fields empty after submit.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:con %>"
ProviderName="<%$ ConnectionStrings:con.ProviderName %>"
SelectCommand="SELECT * FROM [View_Intakesheet]"
InsertCommand="INSERT INTO [View_Intakesheet] [date_withdrawn], [date_delivered], [qc_by], [qc_date], [additional_notes], [Case_Status_Text], [Case_Type_Text]) VALUES (#date_withdrawn, #date_delivered, #qc_by, #qc_date, #additional_notes, #Case_Status_Text, #Case_Type_Text)"
UpdateCommand="UPDATE intakesheet SET date_withdrawn= #date_withdrawn, date_delivered= #date_delivered, qc_by = #qc_by, qc_date=#qc_date, additional_notes = #additional_notes WHERE (case_number = #case_number)">
<InsertParameters>
<asp:Parameter Name="date_withdrawn" />
<asp:Parameter Name="date_delivered" />
<asp:Parameter Name="qc_by" />
<asp:Parameter Name="qc_date" />
<asp:Parameter Name="additional_notes" />
<asp:Parameter Name="Case_Status_Text" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="assigned_to" SessionField="Name" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="case_status" />
<asp:Parameter Name="date_withdrawn" />
<asp:Parameter Name="date_delivered" />
<asp:Parameter Name="qc_by" />
<asp:Parameter Name="qc_date" />
<asp:Parameter Name="additional_notes" />
<asp:Parameter Name="case_number" />
<asp:Parameter Name="case_type" />
</UpdateParameters>
</asp:SqlDataSource>
You can do it by rebinding the gridview like this just after where your update is done.
GridViewIs.DataBind()
You will have to re-bind the grid once you are done with changes. Please let us know if you are doing that.
I am currently working in Visual Basic 2010 with a webform. I created an SQL database inside of Visual Basic and I'm trying to make the insert command move values from the textboxes into the database.
So my question is: how do I get the values from the textboxes (i.e:NameTextBox, AddressTextBox..) and associate them with the parameters (#Name, #Address1)?
The database works, I tested it by inputing values through the code.
Here is the relevant code:
<script runat="server">
' InsertShipper
Private Sub InsertShipper (ByVal Source As Object, ByVal e As EventArgs)
SqlDataSource1.Insert()
End Sub
</script>
<asp:Button ID="OrderButton" runat="server" onclick="InsertShipper" Text="Order" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:OrderListConnectionString %>"
DeleteCommand="DELETE FROM [ORDER] WHERE [OID] = #OID"
InsertCommand="INSERT INTO [ORDER] ([Name], [Address1], [Address2], [City], [State], [Zip], [Phone], [Email], [Subtotal], [Shipping]) VALUES ( #Name, #Address1, #Address2, #City, #State, #Zip, #Phone, #Email, #Subtotal, #Shipping)"
SelectCommand="SELECT * FROM [ORDER]"
UpdateCommand="UPDATE [ORDER] SET [Name] = #Name, [Address1] = #Address1, [Address2] = #Address2, [City] = #City, [State] = #State, [Zip] = #Zip, [Phone] = #Phone, [Email] = #Email, [Subtotal] = #Subtotal, [Shipping] = #Shipping WHERE [OID] = #OID">
<DeleteParameters>
<asp:Parameter Name="OID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter name="Name" Type="String" />
<asp:Parameter name="Address1" Type="String" />
<asp:Parameter Name="Address2" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="Zip" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Subtotal" Type="String" />
<asp:Parameter Name="Shipping" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Address1" Type="String" />
<asp:Parameter Name="Address2" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="State" Type="String" />
<asp:Parameter Name="Zip" Type="String" />
<asp:Parameter Name="Phone" Type="String" />
<asp:Parameter Name="Email" Type="String" />
<asp:Parameter Name="Subtotal" Type="String" />
<asp:Parameter Name="Shipping" Type="String" />
<asp:Parameter Name="OID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Try setting the parameters' ControlID and PropertyName as such:
<asp:ControlParameter Name="Title"
ControlID="txtFoo"
PropertyName="Text"/>
Refer to http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx
Your parameters' Name property are missing the "#" character. You need to add this or it sees it as a completely different parameter.
I have the following SqlDataSource on a GridView. All works except for Delete Function.
What am I doing wrong , no error is given but no result either.
How can this be solved?
<asp:SqlDataSource ID="BookingsSQL" runat="server" ConnectionString="<%$ ConnectionStrings:BookingsConnectionString %>"
OldValuesParameterFormatString="original_{0}" ConflictDetection="OverwriteChanges"
SelectCommand="SELECT [ID], [programme name] AS programme_name, [Start], [Finish], [Source], [Destination], [Comment] FROM [Bookings]"
DeleteCommand="DELETE FROM [Bookings] WHERE [programme name] = #ID" InsertCommand="INSERT INTO [Bookings] ([programme name], [Start], [Finish], [Source], [Destination], [Comment]) VALUES (#programme_name, #Start, #Finish, #Source, #Destination, #Comment)"
UpdateCommand="UPDATE [Bookings] SET [programme name] = #programme_name, [Start] = #Start, [Finish] = #Finish, [Source] = #Source, [Destination] = #Destination, [Comment] = #Comment WHERE [ID] = #ID">
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="programme_name" Type="String" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ID" Type="Int32" />
<asp:Parameter Name="Start" Type="DateTime" />
<asp:Parameter Name="Finish" Type="DateTime" />
<asp:Parameter Name="Source" Type="String" />
<asp:Parameter Name="Destination" Type="String" />
<asp:Parameter Name="Comment" Type="String" />
<asp:Parameter Name="programme_name" Type="String" />
</UpdateParameters>
I think this:
DELETE FROM [Bookings] WHERE [programme name] = #ID
should be this:
DELETE FROM [Bookings] WHERE [ID] = #ID
You delete by [programme name] where from your other queries it looks like you want to delete by the [ID] column.
Since that columns exists, you won't get an error but also no matches to delete. Change the query to:
DELETE FROM Bookings WHERE ID=#ID
I am creating a website now using Microsoft Visual Web Developer 2010 Express and I used the toolbox of "createUserWizard". Then I put the userId for my "CustomerInfo" table to the "data type=uniqueidentifier" because I need to link it to the user name in the aspnet_ table.
Later on, I need to link my "Order" table to the "CustomerInfo" table so I put my orderId data type=uniqueidentifier. Then, I plan insert my order details to the "Order" table but I have the problem of:
"Implicit conversion from data type
sql_variant to uniqueidentifier is not
allowed. Use the CONVERT function to
run this query".
I search and find some answer like datatype of the parameter set to "Empty"or delete it. but then I have this error
" Conversion failed when converting from character string to uniqueidentifier."
This is my SQL
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Order] WHERE [orderId] = #orderId"
InsertCommand="INSERT INTO [Order] ([orderId], [userId], [Services], [PickUpDate], [PickUpTime], [SpecialDate], [SpecialTime]) VALUES (#orderId, #userId, #Services, #PickUpDate, #PickUpTime, #SpecialDate, #SpecialTime)"
SelectCommand="SELECT * FROM [Order]"
UpdateCommand="UPDATE [Order] SET [userId] = #userId, [Services] = #Services, [PickUpDate] = #PickUpDate, [PickUpTime] = #PickUpTime, [SpecialDate] = #SpecialDate, [SpecialTime] = #SpecialTime WHERE [orderId] = #orderId">
<DeleteParameters>
<asp:Parameter Name="orderId" Type="Object" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="orderId" Type="Object" />
<asp:Parameter Name="userId" Type="Object" />
<asp:Parameter Name="Services" Type="String" />
<asp:Parameter Name="PickUpDate" Type="String" />
<asp:Parameter Name="PickUpTime" Type="String" />
<asp:Parameter Name="SpecialDate" Type="String" />
<asp:Parameter Name="SpecialTime" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="userId" Type="Object" />
<asp:Parameter Name="Services" Type="String" />
<asp:Parameter Name="PickUpDate" Type="String" />
<asp:Parameter Name="PickUpTime" Type="String" />
<asp:Parameter Name="SpecialDate" Type="String" />
<asp:Parameter Name="SpecialTime" Type="String" />
<asp:Parameter Name="orderId" Type="Object" />
</UpdateParameters>
</asp:SqlDataSource>
Now that I think of it, maybe there is a problem with my code in vb.
I will put it here, please tell me how to do the correct way of inserting data to the database.
Protected Sub OrderButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderButton.Click
SqlDataSource1.InsertParameters(0).DefaultValue = Now
SqlDataSource1.Insert()
Response.Redirect("~/Customer/AfterOrder.aspx")
End Sub
If you want to insert GUID from your code, you should make parameter type GUID
<asp:Parameter Name="ParamName" DbType="Guid" />
And you should pass GUID value as (c#):
SqlDataSource1.InsertParameters["ParamName"].DefaultValue = System.Guid.NewGuid().ToString();
If you want to pass a string value, you have to use CONVERT(UNIQUEIDENTIFIER,'YOUR VALUE') in the insert query like this:
InsertCommand="INSERT INTO [Order] ([UserId], [etc])
VALUES (CONVERT(UNIQUEIDENTIFIER,#UserId), #etc)"
I am using sqldatasource to binding to the grid.
SqlDatasource is like this:
<asp:SqlDataSource ID="DS_ValidUsers" runat="server"
ConnectionString="<%$ ConnectionStrings:conString %>"
ProviderName="<%$ ConnectionStrings:conString.ProviderName %>"
SelectCommand="SELECT ValidUsers.ID, ValidUsers.DomainID, ValidUsers.isAdmin, ValidUsers.isSubmitToTimberline, ValidUsers.isHistoryDetails, ValidUsers.isSearch, ValidUsers.isAttachments, ValidUsers.isUpload, ValidUsers.isDelete, a.Name AS FullName, a.DepartmentCode FROM ValidUsers INNER JOIN db_common.dbo.DomainUsers_Active AS a ON a.DomainID = ValidUsers.DomainID WHERE (ISNULL(a.DomainID, '') NOT IN ('', 'XX0000'))"
UpdateCommand="UPDATE ValidUsers SET isAdmin = ?, isSubmitToTimberline = ?, isHistoryDetails = ?,isSearch = ?, isAttachments = ?, isUpload = ?, isDelete = ? WHERE (ID = ?)"
DeleteCommand="DELETE FROM ValidUsers WHERE (ID = ?)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="false" Name="isAdmin" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSubmitToTimberline" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isHistoryDetails" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSearch" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isAttachments" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isUpload" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isDelete" Type="Boolean" />
<asp:Parameter DefaultValue="0" Name="original_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>When I click delete, I am getting following error. I don’t have a clue where this
parameter 1 and 2 are coming from?
Microsoft JScript runtime error:
Sys.WebForms.PageRequestManagerServerErrorException:
Command parameter[1] '' is invalid.
Command parameter[2] '' is invalid.
Thanks!
ConnectionString="<%$ ConnectionStrings:conString %>"
ProviderName="<%$ ConnectionStrings:conString.ProviderName %>"
SelectCommand="SELECT ValidUsers.ID, ValidUsers.DomainID, ValidUsers.isAdmin, ValidUsers.isSubmitToTimberline, ValidUsers.isHistoryDetails, ValidUsers.isSearch, ValidUsers.isAttachments, ValidUsers.isUpload, ValidUsers.isDelete, a.Name AS FullName, a.DepartmentCode FROM ValidUsers INNER JOIN db_common.dbo.DomainUsers_Active AS a ON a.DomainID = ValidUsers.DomainID WHERE (ISNULL(a.DomainID, '') NOT IN ('', 'XX0000'))"
UpdateCommand="UPDATE ValidUsers SET isAdmin = ?, isSubmitToTimberline = ?, isHistoryDetails = ?,isSearch = ?, isAttachments = ?, isUpload = ?, isDelete = ? WHERE (ID = ?)"
DeleteCommand="DELETE FROM ValidUsers WHERE (ID = ?)">
<DeleteParameters>
<asp:Parameter Name="ID" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter DefaultValue="false" Name="isAdmin" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSubmitToTimberline" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isHistoryDetails" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isSearch" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isAttachments" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isUpload" Type="Boolean" />
<asp:Parameter DefaultValue="false" Name="isDelete" Type="Boolean" />
<asp:Parameter DefaultValue="0" Name="original_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
So is update working properly? ID is the primary Key right?
Try setting DataKeyName="ID" for your GV and remove the DeleteParameters from SqlDataSource.
Edit:
I would suggest you to follow this tutorial to setup your sqldatasource for Update and Delete. It could be some parameters issue. I would also suggest to handle SqlDataSource Update event and check for e.Exception to info.