简体   繁体   中英

error while trying to retrieve record from database through wcf

I created a wcf service and defined a method to retrieve record from mysql database. When I call the method from windows form application, it throws an exception. It says that the null value was not handled. I have two different issues. One is obviously how to handle the null values and the other is that the method is not working as there is a record in my database so it should not receive null value.

If I define the method within the form application it works fine but not through WCF service. I am using mysql for database.

Wcf code is below:

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 Form Application

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

        }



    }
}

Error Message

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: 

The problem is that you are creating the service reference on init of the class, before the constructor.

The creation of the service reference uses values in the config file, but the config file is not loaded at this time.

Try placing these lines together:

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

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

        ds = obj.SelectEmpDetails();

        //.....
}

You dont need cmd.ExecuteNonQuery() when using a dataset

Look at the following post Connecting to a Mysql DB with C# - Need some with Datasets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM