Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello friends,

iam developing one android application, but i want to insert a values into mysql using wcf services
please suggest me

thanking you.

here is my code:


android app code:
Java
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		 
	   	nameValuePairs.add(new BasicNameValuePair("fullname",s1));
	   	nameValuePairs.add(new BasicNameValuePair("bloodgroup",s6));
	   	nameValuePairs.add(new BasicNameValuePair("dob",s3));
	   	nameValuePairs.add(new BasicNameValuePair("gender",s7));
	   	nameValuePairs.add(new BasicNameValuePair("phoneno",s2));
	   	nameValuePairs.add(new BasicNameValuePair("emailid",s4));
	   	nameValuePairs.add(new BasicNameValuePair("fulladdress",s5));
	   	
	   	
	   	String url = "http://localhost:49910/Service1.svc";
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        JSONObject json = new JSONObject();

            HttpPost post = new HttpPost(url);            
            json.put("fullname",s1);
            json.put("bloodgroup",s6);
            json.put("dob",s3);
            json.put("gender",s7);
            json.put("phoneno",s2);
            json.put("emailid",s4);
            json.put("fulladdress",s5);

            StringEntity se = new StringEntity(json.toString());
            se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
            post.setEntity(se);
            response = client.execute(post);

}



service1.svc.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Web.Services.Description;
using System.Data;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;


namespace bloodservices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        
        MySqlConnection con = new MySqlConnection("datasource=localhost;database=blood_donar;userid=root;password=vishwas7890");
        MySqlCommand cmd;
        string Message;
        public string InsertUserDetails(UserDetails userInfo)
    {
        
   cmd = new MySqlCommand("insert into registration_tab(fullname,bloodgroup,dob,gender,phoneno,emailid,fulladdress) values(@fullname,@bloodgroup,@dob,@gender,@phoneno,@emailid,@fulladdress)", con);
     con.Open();
       cmd.Parameters.AddWithValue("@fullname", userInfo.Fullname);
       cmd.Parameters.AddWithValue("@bloodgroup", userInfo.Bloodgroup);
       cmd.Parameters.AddWithValue("@dob", userInfo.Dob);
      cmd.Parameters.AddWithValue("@gender", userInfo.Gender);
      cmd.Parameters.AddWithValue("@phoneno", userInfo.Phoneno);
       cmd.Parameters.AddWithValue("@emailid", userInfo.Emailid);
       cmd.Parameters.AddWithValue("@fulladdress", userInfo.Fulladdress);

        //int result = cmd.ExecuteNonQuery();

       if (result == 1)
      {

            Message=" Details inserted successfully";

    }

      else
       {

           Message = " Details not inserted successfully";

      }

       con.Close();

        return Message;
    }


Iservice1.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace bloodservices
{
    
    [ServiceContract]
    public interface IService1
    {        
        string InsertUserDetails(UserDetails userInfo);
    }
Posted
Updated 19-Nov-14 20:25pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900