[英]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作为参数,则需要使用DBNull
( http://msdn.microsoft.com/en-us/library/system.dbnull.aspx )
Jason 杰森
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.