![](/img/trans.png)
[英]There's no error but data is not inserted to sql server table using ASP.NET?
[英]Values not inserted in SQL Server DB using ASP.NET WebService
我是編程新手,剛從Microsoft Visual Studio 2017中的ASP.NET C#開始使用WebService。我還將KSoap2下載到Android Studio中,並計划通過WebService將用戶數據插入SQL Server DB。 但是,什么也沒發生。 如果你們所有人都可以看一下我的代碼並提供有關錯誤之處的反饋,那將是很棒的。 謝謝你們!!
這是我的ASP.NET WebService代碼:
[WebMethod]
public Boolean InsertUser(string firstName, string lastName, string email, string password)
{
SqlConnection conn = ConnectionManager.GetConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO dbo.UserData(firstName, lastName, email, password) VALUES('" + firstName + "', '" + lastName + "', '" + email + "', '" + password + "')", conn);
cmd.ExecuteNonQuery();
conn.Close();
return true;
}
這是我在Android Studio中的代碼:
public class TestCreateAcc extends AppCompatActivity {
TextView tvS;
EditText etFN, etLN, etE, etP, etCP;
Button btnSub;
String firstName, lastName, email, password, confirmPwd;
String displayText;
private static final String SOAP_ACTION = "http://tempuri.org/InsertUser";
private static final String OPERATION_NAME = "InsertUser";
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
private static final String SOAP_ADDRESS = "http://10.0.1.86:53877/Service.asmx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_create_acc);
tvS = (TextView) findViewById(R.id.tvS);
btnSub = (Button) findViewById(R.id.btnSub);
btnSub.setOnClickListener( new OnClickListener(){
public void onClick(View v)
{
etFN = (EditText) findViewById(R.id.etFN);
etLN = (EditText) findViewById(R.id.etLN);
etE = (EditText) findViewById(R.id.etE);
etP = (EditText) findViewById(R.id.etP);
etCP = (EditText) findViewById(R.id.etCP);
firstName = etFN.getText().toString().trim();
lastName = etLN.getText().toString().trim();
email = etE.getText().toString().trim();
password = etP.getText().toString().trim();
confirmPwd = etCP.getText().toString().trim();
if(firstName.isEmpty() && lastName.isEmpty() && email.isEmpty() && password.isEmpty() && confirmPwd.isEmpty())
{
tvS.setVisibility(View.VISIBLE);
tvS.setText("Fields cannot be left empty!!!");
tvS.setTextColor(Color.parseColor("#FF0000"));
}
else if(!confirmPwd.equals(password))
{
tvS.setVisibility(View.VISIBLE);
tvS.setText("Passwords do not match!!!");
tvS.setTextColor(Color.parseColor("#FF0000"));
}
else
{
AsyncCallWS task = new AsyncCallWS();
task.execute();
}
}
});
}
class AsyncCallWS extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute(){
tvS.setVisibility(View.INVISIBLE);
}
@Override
protected String doInBackground(String... params){
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
PropertyInfo FirstName = new PropertyInfo();
FirstName.setName("firstName");
FirstName.setValue(params[0]);
FirstName.setType(String.class);
PropertyInfo LastName = new PropertyInfo();
LastName.setName("lastName");
LastName.setValue(params[1]);
LastName.setType(String.class);
PropertyInfo Email = new PropertyInfo();
Email.setName("email");
Email.setValue(params[2]);
Email.setType(String.class);
PropertyInfo Password = new PropertyInfo();
Password.setName("password");
Password.setValue(params[3]);
Password.setType(String.class);
request.addProperty(FirstName);
request.addProperty(LastName);
request.addProperty(Email);
request.addProperty(Password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.debug = true;
try{
httpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String res = response.toString() + "\n";
res += httpTransport.requestDump + "\n" + httpTransport.responseDump;
return res;
}catch(Exception ex){
return ex.toString();
}
}
@Override
protected void onProgressUpdate(String... values){
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
tvS.setVisibility(View.VISIBLE);
displayText = "Success!!";
tvS.setTextColor(Color.parseColor("#00FF00"));
tvS.setText(displayText);
Intent register = new Intent(TestCreateAcc.this, DriverLicenseActivity.class);
startActivity(register);
}
}
謝謝!! :)
編輯:我正在使用和實際的Android設備,這些是我面臨的錯誤:
org.apache.http.conn.HttpHostConnectException:與http://mobilesystemservice.com的連接被拒絕返回的數據不能為null! java.lang.ArrayIndexOutOfBoundsException:length = 0; 索引= 1
我不確定您是否從param
參數中獲取任何值。 但是您可以做的是首先檢查將所有代碼放入try-catch
塊中,然后檢查是否從param
了任何值,然后只需將參數設置為簡單字符串即可。
protected String doInBackground(String... params)
{
try
{
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);
//add properties to SOAP object
//http://kobjects.org/ksoap2/doc/api/org/ksoap2/serialization/SoapObject.html#addProperty(java.lang.String, java.lang.Object)
request.addProperty("firstname", "");
request.addProperty("lastName", "");
request.addProperty("email", "");
request.addProperty("password", "");
int increment = 0;
//use foreach to avoid out of bound index
foreach(String p : params)
{
if (p != null)
{
//set properties to soap object
//http://kobjects.org/ksoap2/doc/api/org/ksoap2/serialization/KvmSerializable.html#setProperty(int, java.lang.Object)
switch(increment)
{
case 0:
request.setProperty(increment, p);
break;
case 1:
request.setProperty(increment, p);
break;
case 2:
request.setProperty(increment, p);
break;
case 3:
request.setProperty(increment, p);
break;
}
}
increment++;
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.debug = true;
httpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
String res = response.toString() + "\n";
res += httpTransport.requestDump + "\n" + httpTransport.responseDump;
return res;
}
catch(Exception ex)
{
return ex.toString();
}
}
只是基於本文檔
希望這會幫助你。
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.