如何处理错误的SOAPEXCEPTION在C#中被解决了

时间:2017-03-20 02:13:13

标签: c# web-services

我正在尝试将我的web服务用于我的Windows应用程序。我的webService 返回数据表。我的Windows应用程序出错。

这是我的代码:

这是我在Web Service中的代码:

[WebMethod]
        public DataTable searchCom(string compCode)
       {
            SqlConnection conn = new SqlConnection("Data Source=LOCALPC\\SQLEXPRESS1; Initial Catalog=Company;Integrated Security=True;");
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand("selectPress", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.Add("@item", SqlDbType.VarChar).Value = compCode;
            cmd.CommandType = CommandType.StoredProcedure;
            da.Fill(dt); 
            return dt;
       }

这是Windows应用程序表单中的代码:

private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = service.searchCom(textBox1.Text);
            if(dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    textBox2.Text = row["CompanyName"].ToString();
                    textBox3.Text = row["balance"].ToString();
                    textBox4.Text = row["maintBalance"].ToString();
                    textBox2.ReadOnly = true;
                    textBox3.ReadOnly = true;
                    textBox4.ReadOnly = true;
                    button1.Visible = false;
                    Back.Visible = true;
                }
            }

        }

3 个答案:

答案 0 :(得分:1)

您应该在Web服务级别处理异常。

[WebMethod]
       public DataTable searchCom(string compCode)
       {
          try
            {
            SqlConnection conn = new SqlConnection("Data Source=LOCALPC\\SQLEXPRESS1; Initial Catalog=Company;Integrated Security=True;");
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand("selectPress", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.Add("@item", SqlDbType.VarChar).Value = compCode;
            cmd.CommandType = CommandType.StoredProcedure;
            da.Fill(dt); 
            return dt;
           }
           catch (Exception ex)
           {
             throw new SoapException("Exception :",   
             SoapException.ServerFaultCode, "SoapException", ex);
           }
       }

然后在消费者端捕获异常

private void button2_Click(object sender, EventArgs e)
        {
         try
            {
            DataTable dt = new DataTable();
            dt = service.searchCom(textBox1.Text);
            if(dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    textBox2.Text = row["CompanyName"].ToString();
                    textBox3.Text = row["balance"].ToString();
                    textBox4.Text = row["maintBalance"].ToString();
                    textBox2.ReadOnly = true;
                    textBox3.ReadOnly = true;
                    textBox4.ReadOnly = true;
                    button1.Visible = false;
                    Back.Visible = true;
                }
            }
           }
         catch (SoapException ex)
         {
            if(ex.Actor == "SoapException")
              //Do something
         }    
        }

答案 1 :(得分:0)

您是否已将WebService设置为以NTLM身份运行?

默认情况下,WebServices在IIS中以匿名身份运行。但是你已经使用集成安全性(也就是当前登录的用户)获得了SQL连接。您是否已将Web服务设置为使用NTLM运行?因为除非您在服务设置和连接期间不受欢迎,否则您将获得异常,因为它无法作为默认IIS用户连接到SQL数据库。

答案 2 :(得分:0)

我尝试将我的网络服务DataTable更改为DataSet,它运行良好。 只是回答我自己的问题:P

[WebMethod]
        public DataSet searchCom(string compCode)
       {
            SqlConnection conn = new SqlConnection("Data Source=LOCALPC\\SQLEXPRESS1; Initial Catalog=Company;Integrated Security=True;");
            conn.Open();
            DataSet dt = new DataSet();
            SqlCommand cmd = new SqlCommand("selectPress", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Parameters.Add("@item", SqlDbType.VarChar).Value = compCode;
            cmd.CommandType = CommandType.StoredProcedure;
            da.Fill(dt); 
            return dt;
       }