简体   繁体   English

参数化查询'((@id nvarchar(4000),@ name nvarchar(4000),@ project nvarchar(4000)')期望参数'@id',未提供

[英]The parameterized query '(@id nvarchar(4000),@name nvarchar(4000),@project nvarchar(4000)' expects the parameter '@id', which was not supplied

while storing data into database through wcf I am getting error. 通过WCF将数据存储到数据库中时出现错误。 I am taking input from android client through textboxes. 我正在通过文本框从android客户端获取输入。 ERROR: The parameterized query '(@ID nvarchar(4000),@Name nvarchar(4000),@Project nvarchar(4000)' expects the parameter '@ID', which was not supplied. 错误:参数化查询'(@ID nvarchar(4000),@ Name nvarchar(4000),@ Project nvarchar(4000)'期望未提供参数'@ID'。

The c# code for data insertion is 用于数据插入的C#代码为

 [DataContract]
    public class studentinfo
    {
        [DataMember(Name = "ID")]
        public string ID { get; set; }

        [DataMember(Name = "Name")]
        public string Name { get; set; }

        [DataMember(Name = "Project")]
        public string Project { get; set; }

        [DataMember(Name = "Result")]
        public string Result { get; set; }


    }

  [OperationContract]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "SaveData")]
        void SaveData(studentinfo studentinfo);



 public void SaveData(studentinfo studentinfo)
    {
        SqlConnection con;


        con = new SqlConnection(ConfigurationManager.ConnectionStrings["Myproject.Properties.Settings.MyConnection"].ConnectionString);

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }



    SqlCommand cmd = new SqlCommand("INSERT INTO Result (ID,Name,Project,Result) values(@ID,@Name,@Project,@Result)", con);

    cmd.Parameters.AddWithValue("@ID", studentinfo.ID);

    cmd.Parameters.AddWithValue("@Name", studentinfo.Name);

    cmd.Parameters.AddWithValue("@Project", studentinfo.Project);

    cmd.Parameters.AddWithValue("@Result", studentinfo.Result);


    int Valid = cmd.ExecuteNonQuery();



    con.Close();

}

Edited: here is android client side code: 编辑:这是android客户端代码:

public class MarksActivity extends Activity {

     private final static String SERVICE_URI = "http://10.0.2.2:51220/Service1.svc";
    Button btnsave, btncal;
    EditText txtid, txtname, txtproject, txtviva, txtpres, txtconfi, txtsystem, txttotal;
    double h=0;
    double s=0;
    double x=0;
    double y=0;
    double z=0;
    String text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.marks);

        btnsave=(Button)findViewById(R.id.btnsave);

        text    = "";

        btncal=(Button)findViewById(R.id.btncal);


        txtid=(EditText)findViewById(R.id.txtid);
        txtname=(EditText)findViewById(R.id.txtname);
        txtproject=(EditText)findViewById(R.id.txtproject);
        txttotal=(EditText)findViewById(R.id.txttotal);

        txtviva=(EditText)findViewById(R.id.txtviva);
        txtpres=(EditText)findViewById(R.id.txtpres);
        txtconfi=(EditText)findViewById(R.id.txtconfi);
        txtsystem=(EditText)findViewById(R.id.txtsystem);

        btnsave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                saveData();
                }
            });


}



    public void saveData() {




        try{

            boolean isValid = true;


                h=Double.parseDouble(txtviva.getText().toString());
                s=Double.parseDouble(txtpres.getText().toString());
                x=Double.parseDouble(txtconfi.getText().toString());
                y=Double.parseDouble(txtsystem.getText().toString());
                  z= h+s+x+y;
                  txttotal.setText(Double.toString(z));




                  if (isValid) {

                        // POST request to <service>/SaveVehicle


                    //Connect to the server
                    HttpPost httpPost=new HttpPost(SERVICE_URI +"/SaveData");
                    httpPost.setHeader("Accept", "application/json");
                    httpPost.setHeader("Content-type", "application/json");

                        // Build JSON string
                        JSONStringer getdata = new JSONStringer()
                            .object()
                                .key("studentinfo")
                                    .object()
                                        .key("ID").value(txtid.getText().toString())
                                        .key("Name").value(txtname.getText().toString())
                                        .key("Project").value(txtproject.getText().toString())
                                        .key("Result").value(txttotal.getText().toString())
                                    .endObject()
                                .endObject();
                        StringEntity entity = new StringEntity(getdata.toString());

                        httpPost.setEntity(entity);

                        // Send request to WCF service
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpResponse response = httpClient.execute(httpPost);

                        Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());

                        // Reload plate numbers

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


        }

    }

I did try some methods provided in other answers but unable to solve this problem. 我确实尝试了其他答案中提供的某些方法,但无法解决此问题。 Please help me to solve it 请帮我解决

It is possible that your studentinfo.ID value is null ? 您的studentinfo.ID值可能为null吗? If you want to pass null as a parameter you need to use DBNull ( http://msdn.microsoft.com/en-us/library/system.dbnull.aspx ) 如果要传递null作为参数,则需要使用DBNullhttp://msdn.microsoft.com/en-us/library/system.dbnull.aspx

Jason 杰森

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数化查询&#39;((@Educator_ID nvarchar(4000),@ Subject_ID…&#39;)期望参数&#39;@Educator_ID&#39;,但未提供 - The parameterized query '(@Educator_ID nvarchar(4000),@Subject_ID …' expects the parameter '@Educator_ID', which was not supplied {“参数化查询&#39;(@Usr_Name nvarchar(4000)…nv&#39;期望参数&#39;@Usr_Name&#39;,未提供。”} - {“The parameterized query '(@Usr_Name nvarchar(4000)… nv' expects the parameter '@Usr_Name', which was not supplied.”} 参数化查询&#39;(@FirstName nvarchar(4000))select * from tblEm,其中FirstName = @&#39;需要参数&#39;@FirstName&#39;,该参数未提供 - The parameterized query '(@FirstName nvarchar(4000))select * from tblEm where FirstName=@' expects the parameter '@FirstName', which was not supplied 参数化查询需要未提供的参数&#39;@Id&#39; - The parameterized query expects the parameter '@Id', which was not supplied 如何指定属性应生成TEXT列而不是nvarchar(4000) - How do I specify that a property should generate a TEXT column rather than an nvarchar(4000) 使用Dapper获取nvarchar(max)会返回一个修剪为4000个字符的字符串。这种行为可以改变吗? - Using Dapper to get nvarchar(max) returns a string trimmed to 4000 characters. Can this behaviour be changed? 需要未提供的参数“@ID”? - expects parameter '@ID', which was not supplied? 参数化查询需要未提供的参数 - The parameterized query expects the parameter , which was not supplied 参数化查询需要未提供的参数 xy - The parameterized query expects parameter xy which was not supplied 参数化查询需要未提供的参数###### - The parameterized query expects the parameter ###### which was not supplied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM