您好朋友我面临一个问题,即将数据加载到网格视图。
页面加载事件调用一个方法,如loaddata()里面我编写代码
using (SqlConnection Sqlcon = new SqlConnection(strCon))
{
using (SqlCommand cmd = new SqlCommand())
{
Sqlcon.Open();
cmd.Connection = Sqlcon;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SP_Marketing";
//cmd.Parameters.Add(new SqlParameter("@Sno", (object) ?? null.Value));
cmd.Parameters.Add(new SqlParameter("@pvchAction", SqlDbType.VarChar,50));
cmd.Parameters["@pvchAction"].Value = "select";
cmd.Parameters.Add("@pIntErrDescOut", SqlDbType.Int).Direction = ParameterDirection.Output;
SqlAda = new SqlDataAdapter(cmd);
ds = new DataSet();
SqlAda.Fill(ds);
GridViewSample.DataSource = ds;
GridViewSample.DataBind();
}
}
找到sqlada中的存储过程记录捕获的错误,如“过程或函数期望参数@dateemailed未提供”
ALTER PROCEDURE SP_Marketing
(
@Sno int =0,
@DateEmailed datetime,
@DateResponded datetime,
@EmailRep varchar(100)=null,
@Type varchar(100)=null,
@Country varchar(100)=null,
@State varchar(100)=null,
@NameoftheCompany varchar(100)=null,
@website varchar(100)=null,
@FirstName varchar(100)=null,
@LastName varchar(100)=null,
@Title varchar(100)=null,
@Email varchar(100)=null,
@Telephone varchar(100)=null,
@Capabilities varchar(100)=null,
@Focus varchar(100)=null,
@pvchCreatedBy varchar(100)=null,
@pvchAction varchar(50)=null,
@pIntErrDescOut int output
)
AS
BEGIN
if(@pvchAction='select')
begin
SELECT sno,Dateemailed,dateresponded,emailrep,[type],country,[state], , nameofthecompany,website,Firstname,Lastname,Title,email,telephone,
capabilities, Focus FROM Emailmarketing WHERE active=1
end
else if(@pvchAction='insert')
begin
INSERT INTO EmailMarketing(DateEmailed,DateResponded,EmailRep,[Type],
Country,[State],NameoftheCompany,website,FirstName,LastName,Title,Email,Telephone,Capabilities,Focus,Createdby,CreatedDt,Active)VALUES(@DateEmailed,@DateResponded,@EmailRep,@Type,
@Country,@State,@NameoftheCompany,@website,@FirstName,@LastName,@Title,@Email,@Telephone,@Capabilities,@Focus,@pvchCreatedBy,GETDATE(),1);
end
else if(@pvchAction='update')
begin
UPDATE EmailMarketing SET DateEmailed=@DateEmailed,DateResponded=@DateResponded,EmailRep=@EmailRep,[Type]=@Type,Country=@Country,[State]=@State,NameoftheCompany=@NameoftheCompany,website=@website,FirstName=@FirstName,LastName=@LastName,Title=@Title,Email=@Email,Telephone=@Telephone,Capabilities=@Capabilities,Focus=@Focus,Updatedby=@pvchCreatedBy,UpdatedDt=GETDATE()
WHERE Sno=@Sno;
end
else if(@pvchAction='delete')
begin
UPDATE EmailMarketing SET Active=@pvchAction WHERE Sno=@Sno;
end
IF (@@ERROR <> 0)
BEGIN
SET @pIntErrDescOut = 1
END
ELSE
BEGIN
SET @pIntErrDescOut = 0
END
END
答案 0 :(得分:2)
以下参数在存储过程中没有默认值:
@DateEmailed datetime,
@DateResponded datetime,
所以你总是需要在代码中提供这些值:
cmd.Parameters.Add(new SqlParameter("@DateEmailed", SqlDbType.DateTime));
cmd.Parameters["@DateEmailed"].Value = DateTime.Now; // Provide your value here
cmd.Parameters.Add(new SqlParameter("@DateResponded", SqlDbType.DateTime));
cmd.Parameters["@DateResponded"].Value = DateTime.Now; // Provide your value here
答案 1 :(得分:1)
如果您正在调用存储过程,除了上面提到的NULL
问题之外,还存在类型不匹配问题。例如,当我提供参数时,我收到了“未提供的Expects参数”错误。
拔出头发一段时间后,我发现程序的输入参数为nvarchar(50)
,而且我传递的字符串比那个长。显然,任何类型的不匹配都报告与未提供相同(在某种程度上我认为是这样)。
因此,您可能需要仔细检查两端的数据类型和大小。