M.L. Martin
asked on
Error on inserting on aspx page with stored procedure
I have an aspx and aspx.vb(code behind page). I am trying to upload values from dropdown list field. I am using a stored procedure in SQL Server 2012. I can get values to upload correctly if I am using int values in the table and the stored procedure. However, when I try and upload varchar values to varchar fields in the table I get the error.....Conversion failed when converting the varchar value 'Weigh-in' to data type int.
Can anyone tell me what I need to change?
ASPX page code
<td>
<asp:DropDownList ID="staffid" runat="server" DataSourceID="StaffNames" DataTextField="staffmember " DataValueField="staffid">
<asp:ListItem Value="home">1</asp:ListIt em>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="groupid" runat="server" DataSourceID="groupname" DataTextField="codedescr" DataValueField="codedescr" >
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="weekid" runat="server" DataSourceID="WeekNumber" DataTextField="codedescr" DataValueField="codedescr" >
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="pointsid" runat="server" DataSourceID="Pointsdesc" DataTextField="codedescr" DataValueField="codedescr" >
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="points_standard" runat="server" Width="50px"></asp:TextBox >
</td>
<td>
<asp:TextBox ID="points_bonus" runat="server" Width="50px"></asp:TextBox >
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Update Points" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Wellnes2 014Connect ionStringo wner %>" InsertCommand="UpdatePoint s" InsertCommandType="StoredP rocedure">
<InsertParameters>
<asp:FormParameter FormField="staffid" Name="staffid" Type="Int32" />
<asp:FormParameter FormField="groupid" Name="groupid" Type="String" />
<asp:FormParameter FormField="weekid" Name="weekid" Type="String" />
<asp:FormParameter FormField="pointsid" Name="pointsid" Type="String" />
<asp:FormParameter FormField="points_standard " Name="points_standard" Type="Int32" />
<asp:FormParameter FormField="points_bonus" Name="points_bonus" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
Code Behind
Partial Class _GroupA
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SqlDataSource1.Insert()
Response.Redirect("~/group a.aspx")
End Sub
End Class
Stored Procedure
PROCEDURE [dbo].[UpdatePoints]
-- Add the parameters for the stored procedure here
@staffid int,
@groupid AS varchar(150),
@weekid varchar(150),
@pointsid varchar(150),
@points_standard int,
@points_bonus int
--BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
---SET NOCOUNT ON;
AS
-- Insert statements for procedure here
INSERT INTO Points
VALUES (@staffid, @groupid, @weekid, @pointsid, @points_standard, @points_bonus)
--END
Can anyone tell me what I need to change?
ASPX page code
<td>
<asp:DropDownList ID="staffid" runat="server" DataSourceID="StaffNames" DataTextField="staffmember
<asp:ListItem Value="home">1</asp:ListIt
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="groupid" runat="server" DataSourceID="groupname" DataTextField="codedescr" DataValueField="codedescr"
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="weekid" runat="server" DataSourceID="WeekNumber" DataTextField="codedescr" DataValueField="codedescr"
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="pointsid" runat="server" DataSourceID="Pointsdesc" DataTextField="codedescr" DataValueField="codedescr"
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="points_standard" runat="server" Width="50px"></asp:TextBox
</td>
<td>
<asp:TextBox ID="points_bonus" runat="server" Width="50px"></asp:TextBox
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Update Points" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Wellnes2
<InsertParameters>
<asp:FormParameter FormField="staffid" Name="staffid" Type="Int32" />
<asp:FormParameter FormField="groupid" Name="groupid" Type="String" />
<asp:FormParameter FormField="weekid" Name="weekid" Type="String" />
<asp:FormParameter FormField="pointsid" Name="pointsid" Type="String" />
<asp:FormParameter FormField="points_standard
<asp:FormParameter FormField="points_bonus" Name="points_bonus" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
Code Behind
Partial Class _GroupA
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SqlDataSource1.Insert()
Response.Redirect("~/group
End Sub
End Class
Stored Procedure
PROCEDURE [dbo].[UpdatePoints]
-- Add the parameters for the stored procedure here
@staffid int,
@groupid AS varchar(150),
@weekid varchar(150),
@pointsid varchar(150),
@points_standard int,
@points_bonus int
--BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
---SET NOCOUNT ON;
AS
-- Insert statements for procedure here
INSERT INTO Points
VALUES (@staffid, @groupid, @weekid, @pointsid, @points_standard, @points_bonus)
--END
I recommend you to print the parameter values before you call the sp and then try to see if it works with your procedure directly from query window. The sp looks straight forward but the mapping of the parameters in the asp code might give problems.
ASKER
Very good point. I did just that and it failed in the stored procedure so I will focus on the SQL.
USE [Wellnes2014]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[UpdatePoints]
@staffid = 1123,
@groupid = N'ee',
@weekid = N'ee',
@pointsid = N'ee',
@points_standard = 345,
@points_bonus = 3
SELECT 'Return Value' = @return_value
GO
************************** ********** ********** ****
Msg 245, Level 16, State 1, Procedure UpdatePoints, Line 34
Conversion failed when converting the varchar value 'ee' to data type int.
USE [Wellnes2014]
GO
DECLARE @return_value int
EXEC @return_value = [dbo].[UpdatePoints]
@staffid = 1123,
@groupid = N'ee',
@weekid = N'ee',
@pointsid = N'ee',
@points_standard = 345,
@points_bonus = 3
SELECT 'Return Value' = @return_value
GO
**************************
Msg 245, Level 16, State 1, Procedure UpdatePoints, Line 34
Conversion failed when converting the varchar value 'ee' to data type int.
Are you sure that your groupid, weekid, and pointsid columns are varchar it appears one of these are the problem during the update.
To track down which one you could give each a unique value rather than all 'ee' so you can find out which one is causing the problem
To track down which one you could give each a unique value rather than all 'ee' so you can find out which one is causing the problem
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Zberteoc
That is exactly my point. There is no conversion to do. I am going to drop and rebuild the table and see what happens.
That is exactly my point. There is no conversion to do. I am going to drop and rebuild the table and see what happens.
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for derrekdeveloper's comment #a39877837
for the following reason:
I dropped the table then recreated it. It is working now.
Accepted answer: 0 points for derrekdeveloper's comment #a39877837
for the following reason:
I dropped the table then recreated it. It is working now.
I don't understand, we got you on the right track, actually pointing to the solution, but no points? I think we are entitled after the time and effort we put in.
I don't understand, we got you on the right track, actually pointing to the solution, but no points? I think we are entitled after the time and effort we put in.
, does this column exist in the table Points?
Suggest in your stored procedure you specify your column names in the insert statement:
INSERT INTO Points (StaffId, GroupId, WeekId, PointsId, PointsStandard, PointsBonus)
VALUES (@staffid, @groupid, @weekid, @pointsid, @points_standard, @points_bonus)
obviously making sure that the column names shown above match your column names in your points table.