我有一系列输入源,我想用它来在我的sql数据库上执行存储过程。存储过程如下所示:
USE [InvoiceSHC]
GO
/****** Object: StoredProcedure [dbo].[UpdateSHCInvoice] Script Date: 02/21/2013 12:03:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[UpdateSHCInvoice]
-- Add the parameters for the stored procedure here
@Ref nvarchar(50),
@PhaseName nvarchar(50),
@PageType nvarchar(50),
@Page float,
@Percent nvarchar(50),
@ChngType nvarchar(50),
@RowCount int output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
insert into MasterData2 ([Ad],[Phase],[Page Type] , [Page Rate], [Percent] , [Change Type] )
values(@Ref, @PhaseName , @PageType , @Page , @Percent ,@ChngType );
/* @@ROWCOUNT returns the number of rows that are affected by the last statement. */
select @RowCount = @@ROWCOUNT
END
我的表单上有一个按钮,提示用户更新表格。这是它的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void MasterData_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=(local);database=InvoiceSHC;Trusted_Connection=Yes");
SqlDataAdapter DA = new SqlDataAdapter("UpdateSHCInvoice", con);
SqlCommand cmd = new SqlCommand("UpdateSHCInvoice", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter Ref = cmd.Parameters.Add("@Ref", SqlDbType.NVarChar, 50);
SqlParameter Phase = cmd.Parameters.Add("@Phase", SqlDbType.NVarChar, 50);
SqlParameter PageType = cmd.Parameters.Add("@PageType", SqlDbType.NVarChar, 50);
SqlParameter Page = cmd.Parameters.Add("@Page", SqlDbType.NVarChar, 50);
SqlParameter Percent = cmd.Parameters.Add("@Percent", SqlDbType.Float );
SqlParameter ChngType = cmd.Parameters.Add("@ChngType", SqlDbType.NVarChar, 50);
Ref.Value = dlRef.SelectedItem;
Phase.Value = dlPhase.SelectedItem;
PageType.Value = dlPageType.SelectedItem;
Page.Value = tbPage.Text;
Percent.Value = dlPercent.SelectedItem;
ChngType.Value = dlChngType.SelectedItem;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
当我取出cmd.ExecuteNonQuery()时,我没有错误,也没有结果。当我在那里时,结果是一个错误System.InvalidCastException:对象必须实现IConvertible。
我为自己的无知而事先道歉,我自学成才,每天都在学习新事物。在大约127年零4天里,我可以理解这一点。
答案 0 :(得分:1)
您正在寻找将值,而不是相应的对象添加到数据库。
像这样纠正你的代码
Ref.Value = dlRef.SelectedItem.Value;
Phase.Value = dlPhase.SelectedItem.Value;
PageType.Value = dlPageType.SelectedItem.Value;
Page.Value = tbPage.Text;
Percent.Value = dlPercent.SelectedItem.Value;
ChngType.Value = dlChngType.SelectedItem.Value;
答案 1 :(得分:1)
除了传递SelectedItem.Value
本身的SelectedItem
之外,如果您在using
块中执行命令(如下所示),则不会需要担心让连接保持打开状态。您还可以使用Parameters.AddWithValue()
删除大量行。
using(SqlConnection con = new SqlConnection("yourConnectionString"))
{
//Not sure why you need a SqlDataAdapter unused here.
//SqlDataAdapter DA = new SqlDataAdapter("UpdateSHCInvoice", con);
SqlCommand cmd = new SqlCommand("UpdateSHCInvoice", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Ref", dlRef.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Phase", dlPhase.SelectedItem.Value);
cmd.Parameters.AddWithValue("@PageType", dlPageType.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Page", tbPage.Text);
cmd.Parameters.AddWithValue("@Percent", dlPercent.SelectedItem.Value);
cmd.Parameters.AddWithValue("@ChngType", dlChngType.SelectedItem.Value);
con.Open();
cmd.ExecuteNonQuery();
}
答案 2 :(得分:0)
在您的存储过程中,@ Page是一个浮点类型,但在您的click方法中,您将tbPage.text指定给该值。你必须投射到正确的类型。