2013-03-13 6 views
0

Я создал службу wcf и определил метод извлечения записи из базы данных mysql. Когда я вызываю метод из приложения формы Windows, он выдает исключение. В нем говорится, что значение null не обрабатывалось. У меня две разные проблемы. Очевидно, что как обращаться с нулевыми значениями, а другой заключается в том, что этот метод не работает, поскольку в моей базе данных есть запись, поэтому он не должен принимать нулевое значение.ошибка при попытке получить запись из базы данных через wcf

Если я определяю метод в приложении формы, он работает нормально, но не через службу WCF. Я использую mysql для базы данных.

ФОС код ниже:

IService1 
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; 

namespace WcfService3 
{ 
    // 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] 
     // string InsertUserDetails(EmpDetails userInfo); 

     [OperationContract] 
     DataSet SelectEmpDetails(); 

     // 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 CompositeType 
    { 
     bool boolValue = true; 
     string stringValue = "Hello "; 

     [DataMember] 
     public bool BoolValue 
     { 
      get { return boolValue; } 
      set { boolValue = value; } 
     } 

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

    [DataContract] 
    public class EmpDetails 
    { 

     int empno; 

     string ename; 

     float sal; 

     int deptno; 

     string email; 



     [DataMember] 

     public int EmpNp 
     { 

      get { return empno; } 

      set { empno = value; } 

     } 



     [DataMember] 

     public string EName 
     { 

      get { return ename; } 

      set { ename = value; } 

     } 

     [DataMember] 

     public float Sal 
     { 

      get { return sal; } 

      set { sal = value; } 

     } 

     [DataMember] 

     public int DeptNo 
     { 

      get { return deptno; } 

      set { deptno = value; } 

     } 

     [DataMember] 

     public string Email 
     { 

      get { return email; } 

      set { email = value; } 

     } 

    } 
} 

Service1.svc

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; 
using MySql.Data.MySqlClient; 
using System.Windows.Forms; 

namespace WcfService3 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together. 
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging. 
    public class Service1 : IService1 
    { 

     private MySqlConnection connection; 
     private string server; 
     private string database; 
     private string uid; 
     private string password; 


     //Constructor 
    public Service1() 
    { 
     Initialize(); 
    } 


    private void Initialize() 
    { 
     server = "192.168.1.222"; 
     database = "cabee"; 
     uid = "user"; 
     password = "Password1"; 
     string connectionString; 
     connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
     database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";"; 

     connection = new MySqlConnection(connectionString); 
    } 
    private bool OpenConnection() 
    { 
     try 
     { 
      connection.Open(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      //When handling errors, you can your application's response based 
      //on the error number. 
      //The two most common error numbers when connecting are as follows: 
      //0: Cannot connect to server. 
      //1045: Invalid user name and/or password. 
      switch (ex.Number) 
      { 
       case 0: 
        MessageBox.Show("Cannot connect to server. Contact administrator"); 
        break; 

       case 1045: 
        MessageBox.Show("Invalid username/password, please try again"); 
        break; 
      } 
      return false; 
     } 
    } 

    //Close connection 
    private bool CloseConnection() 
    { 
     try 
     { 
      connection.Close(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      MessageBox.Show(ex.Message); 
      return false; 
     } 
    } 

     public string GetData(int value) 
     { 
      return string.Format("You entered: {0}", value); 
     } 

     public CompositeType GetDataUsingDataContract(CompositeType composite) 
     { 
      if (composite == null) 
      { 
       throw new ArgumentNullException("composite"); 
      } 
      if (composite.BoolValue) 
      { 
       composite.StringValue += "Suffix"; 
      } 
      return composite; 
     } 

     public DataSet SelectEmpDetails() 
     { 
      DataSet ds = new DataSet(); 

      if (this.OpenConnection() == true) 
      { 

       MySqlCommand cmd = new MySqlCommand("Select * from empinfo", connection); 

       MySqlDataAdapter sda = new MySqlDataAdapter(cmd);   

       sda.Fill(ds); 

       cmd.ExecuteNonQuery(); 

       this.CloseConnection(); 

       return ds; 
      } 
      else 
      { 
       return ds; 

      } 
     } 

    } 

} 

Форма приложения Windows

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using MySql.Data.MySqlClient; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 

     private MySqlConnection connection; 
     private string server; 
     private string database; 
     private string uid; 
     private string password; 

     ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); // Add service reference 

     public Form1() 
     { 
       showdata(); 
     } 



     private void showdata() // to show the data in the DataGridView 

     { 

      DataSet ds = new DataSet(); 

      ds = obj.SelectEmpDetails(); 


      dataGridView1.DataSource = ds.Tables[0]; 

      dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 

     } 



    } 
} 

Сообщение об ошибке

System.NullReferenceException was unhandled 
    HResult=-2147467261 
    Message=Object reference not set to an instance of an object. 
    Source=WindowsFormsApplication1 
    StackTrace: 
     at WindowsFormsApplication1.Form1.showdata() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 100 
     at WindowsFormsApplication1.Form1..ctor() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 29 
     at WindowsFormsApplication1.Program.Main() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+0

Anup, какой линии, когда вы ' Отлаживайте код, вы получаете эту ошибку. 'Вы отлаживаете код, вы не ..?' – MethodMan

+0

Я думаю, что ошибка появляется здесь 'ds.Tables [0]', поскольку набор данных не содержит никакой таблицы – sll

+1

Похоже, что ваша служба obj равна null. –

ответ

0

Проблема в том, что вы создаете ссылку на службу для init класса перед конструктором.

Создание ссылки на службу использует значения в файле конфигурации, но конфигурационный файл в это время не загружается.

Попытка размещения этих линий вместе:

private void showdata() // to show the data in the DataGridView 
{ 

     DataSet ds = new DataSet(); 
     ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); 

     ds = obj.SelectEmpDetails(); 

     //..... 
} 
Смежные вопросы