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

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.

Related

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

StoredProcedure returning multiple resultset Sql To Linq using designer

I want to get multiple resultsets from a storedProc using sql to linq. I was not able to generate it from designer so I wrote below code in designer.cs file. But whenever I add something to designer, it refreshes the designer with the markup in .dbml file and hence it removes the below code every time I add something. I have to copy it every time. If I can get corresponding dbml markup for this, it would be great.
[Function(Name = "dbo.GetAllModulesAndOptions")]
[ResultType(typeof(Module))]
[ResultType(typeof(ModuleOption))]
public IMultipleResults GetAllModules()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((IMultipleResults)(result.ReturnValue));
}
I've already defined Module and ModuleOption as tables.
Now when I add below markup in .dbml file it complains
DBML1114: The Name attribute 'Module' of the Type element is already used by another type.
<Function Name="dbo.GetAllModulesAndOptions" Method="GetAllModules">
<ElementType Name="Module">
<Column Name="ModuleId" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
<Column Name="ModuleName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
<Column Name="Description" Type="System.String" DbType="VarChar(255)" CanBeNull="true" />
<Column Name="SalesDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
<Column Name="ParentModuleId" Type="System.Int32" DbType="Int" CanBeNull="true" />
</ElementType>
<ElementType Name="ModuleOption">
<Column Name="ModuleOptionId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
<Column Name="ModuleOptionName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
<Column Name="ModuleOptionDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
<Column Name="DefaultPrice" Type="System.Decimal" DbType="Money" CanBeNull="true" />
<Column Name="ModuleId" Type="System.Int64" DbType="BigInt" CanBeNull="true" />
<Column Name="InUse" Type="System.Int32" DbType="Int" CanBeNull="true" />
</ElementType>
</Function>
I'm using Visual Studio 2008 SP1
Add the method to the partial class for the data context.
You achieve this by adding a file with the same name as the data context alongside the dbml file, and use the class declaration:
public partial class YourDataContext
{
[Function(Name = "dbo.GetAllModulesAndOptions")]
[ResultType(typeof(Module))]
[ResultType(typeof(ModuleOption))]
public IMultipleResults GetAllModules()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo (MethodInfo.GetCurrentMethod())));
return ((IMultipleResults)(result.ReturnValue));
}
}
I'm replying my own answer.
One cannot use the already defined type for result set of stored procedure. So I had to change the name of ElementType to ModuleResult and ModuleOptionResult.
<Function Name="dbo.GetAllModulesAndOptions" Method="GetAllModules">
<ElementType Name="ModuleResult">
<Column Name="ModuleId" Type="System.Int64" DbType="BigInt NOT NULL" CanBeNull="false" />
<Column Name="ModuleName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
<Column Name="Description" Type="System.String" DbType="VarChar(255)" CanBeNull="true" />
<Column Name="SalesDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
<Column Name="ParentModuleId" Type="System.Int32" DbType="Int" CanBeNull="true" />
</ElementType>
<ElementType Name="ModuleOptionResult">
<Column Name="ModuleOptionId" Type="System.Int32" DbType="Int NOT NULL" CanBeNull="false" />
<Column Name="ModuleOptionName" Type="System.String" DbType="VarChar(50)" CanBeNull="true" />
<Column Name="ModuleOptionDesc" Type="System.String" DbType="VarChar(MAX)" CanBeNull="true" />
<Column Name="DefaultPrice" Type="System.Decimal" DbType="Money" CanBeNull="true" />
<Column Name="ModuleId" Type="System.Int64" DbType="BigInt" CanBeNull="true" />
<Column Name="InUse" Type="System.Int32" DbType="Int" CanBeNull="true" />
</ElementType>
</Function>
Here are the steps which I took to solve the above problem.
Delete the .designer.cs file
Add above markup to the .dbml file
Exclude .dbml and .dbml.layout file
Include .dbml and .dbml.layout file (this will generate .designer.cs file again but will not include it in the project).
Include .designer file in project.
Get the list of Module type and ModuleOption type as below.
var modules = from row in results.GetResult<ModuleResult>().ToList()
select new Module
{
ModuleId = row.ModuleId,
ModuleName = row.ModuleName,
Description = row.Description,
SalesDesc = row.SalesDesc,
ParentModuleId = row.ParentModuleId
};
var moduleOptions = from row in results.GetResult<ModuleOptionResult>().ToList()
select new ModuleOption
{
ModuleOptionId = row.ModuleOptionId,
ModuleOptionName = row.ModuleOptionName,
ModuleOptionDesc = row.ModuleOptionDesc,
DefaultPrice = row.DefaultPrice,
ModuleId = row.ModuleId,
InUse = row.InUse
};
UPDATE
Still a better way.
Right click the dbml file in solution explorer and select open with. Choose XML Editor and when you save the file within visual studio it automatically generates the designer.cs file.

delete from gridview using stored procedure

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.

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