如何修复此错误"未处理的类型' System.StackOverflowException'发生在demoWcfService.DLL"

时间:2014-08-27 07:01:03

标签: asp.net sql-server wcf

我已经创建了一个wcf服务并从Web应用程序中使用它。我已经定义了WCF中的所有数据库连接并从web应用程序调用。 它在此页面的I Service1.cs中显示错误它显示错误在demoWcfService中发生了未处理的类型'System.StackOverflowException'的异常.DLL“此时”公共字符串移动“

IService1.cs:

public interface IService1
{
    [OperationContract]
    string insertuserdetails(userdetails userinfo);
}

[DataContract]
public class userdetails
{
    [DataMember]
    public string name
    {
       get { return name; }
       set { name = value; }
    }

    [DataMember]
    public string password
    {
       get { return password; }
       set { password = value; }
    }

    [DataMember]
    public string mobile
    {
       get { return mobile; }
       set { mobile = value; }
    }
}

Service1.svc.cs:

public class Service1 : IService1
{
    public string insertuserdetails(userdetails userinfo)
    {
        string message;
        SqlConnection con = new SqlConnection(" Data Source=72.18.355.116,1533;Initial    
        Catalog=trainingcyber;User Id=cyber13;Password=train13cyber$$;");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into info (name,password,mobile) 
       values(@name,@password,@mobile) ");

        cmd.Parameters.AddWithValue("@name", userinfo.name);
        cmd.Parameters.AddWithValue("@password", userinfo.password);
        cmd.Parameters.AddWithValue("@mobile", userinfo.mobile);
        int result = cmd.ExecuteNonQuery();
        if (result == 1)
        {
            message = userinfo.name + " inforamation inserted ";
        }
        else
        {
            message = userinfo.name + "not inserted";
        }

        con.Close();
        return message;


    }

Web应用程序代码:Default.aspx.cs:

public partial class _Default : System.Web.UI.Page
{
   ServiceReference1.Service1Client objServiceClientobjService = new    ServiceReference1.Service1Client();

   protected void Page_Load(object sender, EventArgs e)
   {

   }

   protected void Button1_Click(object sender, EventArgs e)
   {
      userdetails userinfo = new userdetails();
      userinfo.name = TextBoxUserName.Text;
      userinfo.password = TextBoxPassword.Text;
      userinfo.mobile = TextBoxMobile.Text;

      string result = objServiceClientobjService.insertuserdetails(userinfo);
      LabelMessage.Text = result;


   }
}

1 个答案:

答案 0 :(得分:0)

您没有在sqlCommand中传递连接字符串,我已经更改了您的方法。

public class Service1 : IService1
{
    public string insertuserdetails(userdetails userinfo)
    {
        string message;
        SqlConnection con = new SqlConnection(" Data Source=72.18.355.116,1533;Initial    
        Catalog=trainingcyber;User Id=cyber13;Password=train13cyber$$;");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into info (name,password,mobile) 
       values(@name,@password,@mobile) ",con);

        cmd.Parameters.AddWithValue("@name", userinfo.name);
        cmd.Parameters.AddWithValue("@password", userinfo.password);
        cmd.Parameters.AddWithValue("@mobile", userinfo.mobile);
        int result = cmd.ExecuteNonQuery();
        if (result == 1)
        {
            message = userinfo.name + " inforamation inserted ";
        }
        else
        {
            message = userinfo.name + "not inserted";
        }

        con.Close();
        return message;


    }