I have a WCF web service. I want to post some values into MS SQL database using IIS. GET method works. But i can not find where i have mistakes in my POST method. Does anyone help me about where are my mistakes... Thanks all. Here are my codes...
[OperationContract] //Interface
[WebInvoke(Method = "POST", UriTemplate = "AddName", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
int AddName(uName name);
[DataContract]
public class uName // a class in Interface
{
string name;
[DataMember]
public string setGetname
{
get { return name; }
set { name = value; }
}
}
public int AddName(uName name) //My POST method in service class
{
int state = 0;
SqlConnection connection = new SqlConnection(this.conStr);
SqlCommand cmd = new SqlCommand();
try
{
if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
cmd = new SqlCommand("Insert into Table_1(kolon1) values(@name)", connection);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@name", name.setGetname);
cmd.ExecuteReader();
state = 1;
}
catch (Exception ex)
{
throw ex;
}
finally
{
connection.Close();
cmd.Dispose();
}
return state;
}
SOLVED-> I changed to "BodyStyle = WebMessageBodyStyle.Bare" to "BodyStyle = WebMessageBodyStyle.Wrapped" , it worked for me ... Thanks all...