delete from gridview using stored procedure - vb.net

Hi i have a gridview, linked to a sqldatasource. I have added a stored procedure to delete from multiple tables, and then enabled deleting on the gridviews smart tag.
When i click the delete button i get an error message, "Object must implement IConvertible". I read that it is a problem passing the parameter to the stored procedure, possibly the wrong datatype being passed. Im not sure if i am passing the parameter to the stored procedure at all. The parameter should be the gridviews datakeyname, in this case it is "UserId".
The stored procedure works i fine in management studio, so i think it is just the parameter being passed (or possibly not being passed)
Do i have to code the parameter in the code behind to be passed to the stored procedure?
<asp:SqlDataSource ID="selectUsers" runat="server" ConnectionString="<%$ ConnectionStrings:CASSFConnectionString %>"
SelectCommand="SELECT aspnet_Membership.UserId, aspnet_Membership.IsLockedOut, aspnet_Membership.Email, aspnet_Membership.CreateDate, aspnet_Membership.LastLoginDate, aspnet_Membership.LastPasswordChangedDate, aspnet_Profile.PropertyValuesString, aspnet_Users.UserName, aspnet_Membership.IsApproved FROM aspnet_Membership INNER JOIN aspnet_Profile ON aspnet_Membership.UserId = aspnet_Profile.UserId INNER JOIN aspnet_Users ON aspnet_Membership.UserId = aspnet_Users.UserId AND aspnet_Profile.UserId = aspnet_Users.UserId WHERE (CONVERT (nvarchar(256), aspnet_Profile.PropertyValuesString) = #district)"
DeleteCommand="DeleteUsers" DeleteCommandType="StoredProcedure" UpdateCommand="UPDATE [aspnet_Membership] SET [IsApproved] = #IsApproved, [Email] = #email, [IsLockedOut] = #IsLockedOut WHERE [aspnet_Membership].[UserId] = #UserID">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="district" PropertyName="SelectedValue" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="UserId" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="IsApproved" />
<asp:Parameter Name="email" />
<asp:Parameter Name="IsLockedOut" />
<asp:Parameter Name="UserID" />
</UpdateParameters>
</asp:SqlDataSource>

Found the answer, i had just to remove Type="String" from the delete parameter.

Related

Procedure has no parameters and arguments were supplied (literally no parameters)

I've been getting errors with this stored procedure so I tried simplifying it right down. For some reason I'm still getting an error saying the SP is expecting a parameter when I'm not using any parameters at all.
I should note that I'm trying to get this working via a Telerik RadDataform in ASP. Could it be that Telerik is adding a sneaky parameter here?
Here's the SP:
ALTER procedure [dbo].[NewEmployee_Update]
as
insert
NewEmployeeDetails
select top 1
NE.FormHeaderID,
NE.VersionNumber + 1,
getdate(), -- AddDate
'vibribbon',
'Bart',
'Simpson',
'El Barto',
'Rascal'
from
OnlineFormHeader H
inner join NewEmployeeDetails NE on NE.FormHeaderID = H.RecID
where
H.RecID = 3
order by
NE.VersionNumber desc
And the ASP DataSource:
<asp:SqlDataSource ID="dsEmployeeDetails" runat="server" ConnectionString="<%$ ConnectionStrings:OnlineStaffFormsConnectionString %>" InsertCommand="NewEmployee_Insert"
InsertCommandType="StoredProcedure" UpdateCommand="NewEmployee_Update" UpdateCommandType="StoredProcedure" SelectCommand="select top 1
NE.*
from
OnlineFormHeader H
inner join NewEmployeeDetails NE on NE.FormHeaderID = H.RecID
where
H.RecID = #formHeaderID
order by
NE.VersionNumber desc">
<SelectParameters>
<asp:QueryStringParameter Name="formHeaderID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:SessionParameter Name="currentUser" SessionField="currentUser" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="PreferredName" Type="String" />
<asp:Parameter Name="JobTitle" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</div>
</asp:Content>
It seems the Telerik DataForm automatically adds the unique id of the record to be updated to the parameter set.
Probably more noticeable here because I'm fudging the "update" command to insert instead.

Passing a Guid to SQL code within Asp.Net

This SQL code is in the Asp.Net code itself so how can I set the WHERE to equal the Guid which I have passed over in the requestQuery String?
E.g I want to put something like WHERE tblEntrants.compID = Request.QueryString("Comp") but obviously I can't. The Guid in the QueryString need passing in to this SQL query somehow. I'm fairly new to all this so if anyone can help I would much appreciate it.
<asp:SqlDataSource ID="DSEntrants" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT tblEntrants.accountID, tblEntrants.compID, tblaccounts.accountID, tblaccounts.contactName, tblEntrants.paid
FROM tblAccounts INNER JOIN
tblEntrants ON tblAccounts.accountID = tblEntrants.accountID
WHERE tblEntrants.compID = ?????????????????
ORDER BY tblEntrants.accountID DESC"></asp:SqlDataSource>
I have just worked out how to do this. You can directly set a querystring parameter like this..
<asp:SqlDataSource ID="DSEntrants" runat="server" ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" SelectCommand="SELECT tblEntrants.accountID, tblEntrants.compID, tblaccounts.accountID, tblaccounts.contactName, tblaccounts.skypeUserName, tblEntrants.paid
FROM tblAccounts INNER JOIN
tblEntrants ON tblAccounts.accountID = tblEntrants.accountID
WHERE tblEntrants.compID = #Event_ID
ORDER BY tblEntrants.accountID DESC">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="compID" Name="Event_ID" />
</SelectParameters>
All works fine, thanks for reading.

Changing SqlDataSource SelectCommand based on control parameters

I'm working on a web app using vb.net and SQL Server 2008 R2
In one of the aspx file, I have this:
<asp:SqlDataSource ID="dsParentUnit" runat="server"
ConnectionString="<%$ ConnectionStrings:CN %>"
SelectCommand="SELECT * FROM [t_Unit] WHERE ([UnitTypeID] = (#UnitTypeID) - 1) AND NOT [UnitTypeID] = 1 ORDER BY UnitName ASC">
<SelectParameters>
<asp:ControlParameter ControlID="cboUnitType" Name="UnitTypeID" PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
in which the datasource dsParentUnit will change based on selected value of a dropdown list cboUnitType.
The datasource sdParentUnit is used for another dropwdown list item.
Now, I want to change the WHERE ([UnitTypeID] = (#UnitTypeID) - 1 so that it becomes something like this pseudo code:
If cboUnitType.SelectedValue <= 5 Then
WHERE ([UnitTypeID] = (#UnitTypeID) - 1
Else
WHERE ([UnitTypeID] = (#UnitTypeID) - 3
Can I do that?
And if possible, I would like to avoid building any stored function in the database, since the required paperwork for modifying database is really long...
Of course, only if it possible.
Thank you :)
Try this:
<asp:SqlDataSource ID="dsParentUnit" runat="server"
ConnectionString="<%$ ConnectionStrings:CN %>"
SelectCommand="SELECT * FROM [t_Unit] WHERE ([UnitTypeID] = (#UnitTypeID) -
<%IIF (cboUnitType.SelectedValue <= 5, "1", "3")%>
) AND NOT [UnitTypeID] = 1 ORDER BY UnitName ASC">
<SelectParameters>
<asp:ControlParameter ControlID="cboUnitType" Name="UnitTypeID" PropertyName="SelectedValue" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>

2 series on my chart

I would like to show 2 series on the same chart, however I'm not sure how to update the following code:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UcSalesSeries.ascx.cs" Inherits="Silverlight.ConfigEnhanced.Web.UcSalesSeries" %>
<asp:Chart ID="Chart1" runat="server" DataSourceID="LinqDataSource1"
Height="500px" Width="750px" >
<Series>
<asp:Series ChartType="Line" Name="Series1" XValueMember="EndOfMonth"
YValueMembers="Quantity" >
</asp:Series>
</Series>
<Series>
<asp:Series ChartType="Line" Name="Series2" XValueMember="EndOfMonth"
YValueMembers="Quantity" >
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas> <Legends>
<asp:Legend TableStyle="Auto" Docking="Top" >
</asp:Legend>
</Legends>
</asp:Chart>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Data.DataClasses1DataContext" EntityTypeName="" Select="new (EndOfMonth, Quantity)"
TableName="T_SalesDatas" OrderBy="EndOfMonth" Where="Model == #Model">
<WhereParameters>
<asp:Parameter DefaultValue="XXS" Name="Model" Type="String" />
</WhereParameters>
</asp:LinqDataSource>
the second serie I would like to see is the same as above, but I would be changing the parameter
<asp:Parameter DefaultValue="NEWVALUE" Name="Model" Type="String" />
The Chart does not support multiple DataSources so you will need to manually add the points from code behind to the chart when rendering the page. You create two tables from your data with the different parameters and iterate each table and adding them to the chart manually.
Read more in the section 'Manual' series population on this MSDN blog:
http://blogs.msdn.com/b/alexgor/archive/2009/02/21/data-binding-ms-chart-control.aspx

Hibernate - New colums from a joined table [duplicate]

This question already has answers here:
Hibernate One-To-Many Unidirectional on an existing DB
(4 answers)
Closed 2 years ago.
I have a class User object. I am trying to load this object from the Database using Hibernate.
My SQL statement is:
SELECT
users.uid AS uid,
users.deleted AS deleted,
users.username AS username,
users.passwd AS passwd,
users.disabled AS disabled,
users.lockout AS lockout,
users.expires AS expires,
data_firstname.value AS firstname,
data_lastname.value AS lastname
FROM
ac_users as users
LEFT JOIN
ac_userdef_data as data_firstname
ON
users.uid = data_firstname.parentuid AND
data_firstname.fieldname like 'firstname'
LEFT JOIN
ac_userdef_data as data_lastname
ON
users.uid = data_lastname.parentuid AND
data_lastname.fieldname like 'lastname'
WHERE
users.deleted = 0
My mapping for the User class is:
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<generator class="native"/>
</id>
<property name="deleted" column="deleted" />
<property name="username" column="username" />
<property name="password" column="passwd" />
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="disabled" column="disabled" />
<property name="lockout" column="lockout" />
<property name="expires" column="expires" />
</class>
My problem is that the table ac_users does not have a column 'firstname' or 'lastname'
These columns, only exist in my resultset from the SQL-join statement.
They do also not exist in the ac_userdef_data table. There i have 2 colums: fieldname and value. and 2 rows:
fieldname = 'firstname' with some value in the value column
and another row with
fieldname = 'lastname' with some value in the value column
How do i change my mapping file, so that Hibernate will understand that it needs to load the firstname and lastname column into my firstname and lastname fields on my POJO, while those columns dont actually exist on the referenced ac_users table.?
I now have some working code. The trick was to specify a loading Query for the User class. Hibernate then validates against the return from the Query instead of the Table design.
Class mapping
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<generator class="native"/>
</id>
<property name="deleted" column="deleted" />
<property name="username" column="username" />
<property name="password" column="passwd" />
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="disabled" column="disabled" />
<property name="lockout" column="lockout" />
<property name="expires" column="expires" />
<loader query-ref="GetAllUsers" />
</class>
My Query declaration
<sql-query name="GetAllUsers">
<return alias="user" class="com.agetor.commons.security.User" />
<![CDATA[
SELECT
users.uid AS uid,
users.deleted AS deleted,
users.username AS username,
users.passwd AS passwd,
users.disabled AS disabled,
users.lockout AS lockout,
users.expires AS expires,
data_firstname.value AS firstname,
data_lastname.value AS lastname
FROM
ac_users as users
LEFT JOIN
ac_userdef_data as data_firstname
ON
users.uid = data_firstname.parentuid AND
data_firstname.fieldname like 'firstname'
LEFT JOIN
ac_userdef_data as data_lastname
ON
users.uid = data_lastname.parentuid AND
data_lastname.fieldname like 'lastname'
WHERE
users.deleted = 0
]]>
</sql-query>
I had to use the line <return alias="user" class="com.agetor.commons.security.User" /> so that the returned collection was typed correctly