I insert data in table by wcf service then comes in error

Posted by Shail12345 under Error and Solution on 11/10/2013 | Points: 10 | Views : 2852 | Status : [Member] | Replies : 5

My code is use.........Iservice1.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfServiceInsertStudent
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{

[OperationContract]
bool InsertStudent(StudentDetails std_det);




// TODO: Add your service operations here
}


// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class StudentDetails
{
int id;
string name;
[DataMember]
public int Id
{
get { return id; }
set { value = id; }
}
[DataMember]
public string Name
{
get { return name; }
set { value = name; }
}

}
}
and Service1.svc.cs................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;

namespace WcfServiceInsertStudent
{
// 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
{
public bool InsertStudent(StudentDetails std_det)
{
bool result = false;
SqlConnection con = new SqlConnection("Data Source=shail-PC;Initial Catalog=College;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into UserTab1(Name)values(@Name)", con);
cmd.Parameters.AddWithValue("@Name", std_det.Name);
result = Convert.ToBoolean(cmd.ExecuteNonQuery());
con.Close();
return result;
}
}
}
and my aspx.cs......code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ServiceReference1;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnsub_Click(object sender, EventArgs e)
{
StudentDetails std_det = new StudentDetails();
std_det.Name = txtname.Text;
obj.InsertStudent(std_det);
}
}
My table is.............
Id int (pk) not null,
Name varchar(50) not null
and error come in service1.svc.cs
The parameterized query '(@Name nvarchar(4000))Insert into UserTab1(Name)values(@Name)' expects the parameter '@Name', which was not supplied.
plz help me




Responses

Posted by: Bandi on: 11/10/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
May be cross check whether you have value in std_det.Name ?
Service1.svc.cs................
 using System; 

using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;

namespace WcfServiceInsertStudent
{
// 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
{
public bool InsertStudent(StudentDetails std_det)
{
bool result = false;
SqlConnection con = new SqlConnection("Data Source=shail-PC;Initial Catalog=College;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into UserTab1(Name)values(@Name)", con);

// Check whether you are getting value of std_det.Name or not? If NOT exist insert sample data for checking purpose only
if (!String.IsNullOrEmpty(std_det.Name))
{
cmd.Parameters.AddWithValue("@Name", std_det.Name);
}
else
{
string fakeValue = "Sample";
cmd.Parameters.AddWithValue("@Name", fakeValue);
}

result = Convert.ToBoolean(cmd.ExecuteNonQuery());
con.Close();
return result;
}
}
}


Let me know the result

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 11/10/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Shail12345,

Just change you code as per below one and it will work.

string Name = "";
SqlConnection con = new SqlConnection("Data Source=shail-PC;Initial Catalog=College;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into UserTab1(Name)values(@Name)", con);
if (!string.IsNullOrEmpty(std_det.Name))
cmd.Parameters.AddWithValue("@Name", std_det.Name);
else
cmd.Parameters.AddWithValue("@Name", string.Empty);


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shail12345 on: 11/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Allemash Sir,
code is run but name field can not insert in table only id store name column blank
plz help me
thanks


Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/11/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
means you are getting null for std_det.Name value...
this code is working as expected( Insert string.Empty value in case of std_det.Name is NULL )
if (!string.IsNullOrEmpty(std_det.Name))
{
cmd.Parameters.AddWithValue("@Name", std_det.Name);
}
else
{
cmd.Parameters.AddWithValue("@Name", string.Empty);
}


Some where std_det.Name value is getting NULL.. Check that one...

Refer this link for WCF Service sample code for Insert operation
http://www.sujitbhujbal.com/2012/10/wcf-example-for-inserting-and.html
http://www.codeproject.com/Tips/468354/WCF-Example-for-Inserting-Deleting-and-Displaying

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 11/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://www.c-sharpcorner.com/uploadfile/rohatash/inserting-data-into-database-using-wcf-service/


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shail12345, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response