参数化查询'(@Educator_ID nvarchar(4000),@ Subject_ID ....'需要参数'@Educator_ID',这是未提供的

时间:2014-09-08 13:05:03

标签: c# wcf

WCF代码

public class Service1 : IService1
{

    public string AdminAssign(AssignEducator edInfo)
    {
        string text = "";
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|NoBrainerDB.mdf;Integrated Security=True");
        con.Open();
        SqlCommand command = new SqlCommand("insert into [EdSubject] (Educator_ID,Subject_ID,ES_Grade,ES_Subject ) values (@Educator_ID,@Subject_ID,@ES_Grade,@ES_Subject);", con);
        command.Parameters.AddWithValue("@Educator_ID", edInfo.Educator_ID);
        command.Parameters.AddWithValue("@Subject_ID", edInfo.Subject_ID);
        command.Parameters.AddWithValue("@ES_Grade", edInfo.ES_Grade);
        command.Parameters.AddWithValue("@ES_Subject", edInfo.ES_Subject);
        int result = command.ExecuteNonQuery();

        if (result == 1)
        {
            text = "User is registered ";
        }
        else
        {
            text = "User is not registered";
        }
        con.Close();
        return text;
    }
}

C#代码

public partial class AdminAssignEducator : System.Web.UI.Page
{
    ServiceReference1.Service1Client objServiceClientobjService = new ServiceReference1.Service1Client();
    protected void Page_Load(object sender, EventArgs e)
    {

        AssignEducator assignEd = new AssignEducator();
        assignEd.ES_Grade = Grade.SelectedValue;
        assignEd.ES_Subject = Subject.SelectedValue;
        assignEd.Subject_ID = Request.QueryString["Subject_ID"];
        assignEd.Educator_ID = Request.QueryString["Educator_ID"];
        string result = objServiceClientobjService.AdminAssign(assignEd);
        Response.Redirect("AdminAssignEducator.aspx");
    }
}

1 个答案:

答案 0 :(得分:0)

这通常是null的问题。如果Educator_ID类型可以是nullstringint?等),那么您需要使用:

command.Parameters.AddWithValue("@Educator_ID",
    (object)edInfo.Educator_ID ?? DBNull.Value);

或用null替换DBNull的其他代码。基本上,.Value null 的输入参数不会被发送。或者,只需使用为您执行此操作的框架。大多数ORM和微观ORM都会自动完成此任务。