为什么我不能使用Web服务和存储产品插入数据库

时间:2015-08-06 07:57:53

标签: c# web-services

当我尝试使用我的网络服务和商店产品向我的数据库插入内容时出现问题。我有这个:

Client.asmx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace WebServicesCRUD
{
    /// <summary>
    /// Summary description for Client
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Client : System.Web.Services.WebService
    {

        [WebMethod]
        public int  InsertDetail(string PersonName,string PersonCity)
        {
                int retRecord = 0;
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("InsertDetail", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("PersonName", SqlDbType.VarChar, 100).Value = PersonName;
                        cmd.Parameters.Add("PersonCity", SqlDbType.VarChar, 100).Value = PersonCity;
                        if (con.State != ConnectionState.Open)
                        {
                            con.Open();

                        }
                        retRecord = cmd.ExecuteNonQuery();
                    }
                }
                return retRecord;
        }
    }
}

WebForm1.aspx.cs中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebServicesCRUD
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            localhost.Client objTest = new localhost.Client();
            int ret = objTest.InsertDetail(txtName.Text, txtCity.Text);
            if (ret > 0)
            {
                lblMessage.Text = "Record Insert Successfully";

            }
            else
            {
                lblMessage.Text = "Error while inserting record";
            }
        }
    }
}

WebForm1.aspx的

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebServicesCRUD.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>ID</td>
            <td><asp:TextBox runat="server" ID="txtID"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Name</td>
            <td><asp:TextBox runat="server" ID="txtName"></asp:TextBox></td>
        </tr>
        <tr>
            <td>City</td>
            <td><asp:TextBox runat="server" ID="txtCity"></asp:TextBox></td>
        </tr>
    </table>
        <asp:Label runat="server" ID="lblMessage">

        </asp:Label>
        <asp:Button runat="server" ID="btnInsert" OnClick="btnInsert_Click" Text="Insert" />
    </div>
    </form>
</body>
</html>

当我填写所有字段,然后单击按钮插入时,给我例外。我该如何解决这个问题?

说明

  

执行当前期间发生了未处理的异常   网络请求。请查看堆栈跟踪以获取更多信息   错误以及它在代码中的起源。

例外详情

> System.Web.Services.Protocols.SoapException:
> System.Web.Services.Protocols.SoapException: Server was unable to
> process request. ---> System.NullReferenceException: Object reference
> not set to an instance of an object.    at
> WebServicesCRUD.Client.InsertDetail(String PersonName, String
> PersonCity) in
> E:\Tin\C#\Source\WebServicesCRUD\WebServicesCRUD\Client.asmx.cs:line
> 27    --- End of inner exception stack trace ---

来源错误:

> Line 76:        
> [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/InsertDetail",
> RequestNamespace="http://tempuri.org/",
> ResponseNamespace="http://tempuri.org/",
> Use=System.Web.Services.Description.SoapBindingUse.Literal,
> 
> Line 77:         public int InsertDetail(string PersonName, string
> PersonCity) { <br>
Line 78:             object[] results =
> this.Invoke("InsertDetail", new object[] { <br>
Line 79:                   
> PersonName, <br>
 Line 80:                         PersonCity});
> ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

1 个答案:

答案 0 :(得分:0)

第27行会建议您错过了来自Web服务web.config

的连接字符串