0

Im following a C# database connection tutorial however the code they provide for the connection class has a problem, it has an error on dat_set which I'm assuming needs to be set as a variable but I'm unsure. Having looked at the code the tutorial provides many times what i have is exactly the same

The errors are these lines

da_1.Fill(dat_set, "Table_Data_1");
return dat_set;

Here is what i have

using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

namespace JBT
{
    class DBConnect
    {
        private string sqlString;
        private string strCon;
        System.Data.SqlClient.SqlDataAdapter da_1;

        public string Sql
        {
            set { sqlString = value; }
        }

        public string connection_string
        {
            set { strCon = value; }
        }

        public System.Data.DataSet GetConnection
        {
            get
            { return MyDataSet(); }
        }

        private System.Data.DataSet MyDataSet()
        {
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);

            con.Open();

            da_1 = new System.Data.SqlClient.SqlDataAdapter(sqlString, con);
            da_1.Fill(dat_set, "Table_Data_1");
            con.Close();

            return dat_set;
        }





    }
}
2
  • Where did you copy this code from? Is it here? homeandlearn.co.uk/csharp/csharp_s12p6.html (seems an exact copy & paste) Commented Sep 28, 2014 at 10:11
  • 3
    You show where the error happens, but you do not say what the error is. Commented Sep 28, 2014 at 10:12

2 Answers 2

1

The Dataset that you want to Fill need to be intialized before

  private System.Data.DataSet MyDataSet()
  {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);

        con.Open();

        da_1 = new System.Data.SqlClient.SqlDataAdapter(sqlString, con);
        System.Data.DataSet dat_set = new System.Data.DataSet();
        da_1.Fill(dat_set, "Table_Data_1");
        con.Close();
        return dat_set;
    }

Of course this could work only if you have initialized the sqlString and the strCon before calling this code. This should be assumed because you say that the error occurs at the Fill line

Sign up to request clarification or add additional context in comments.

1 Comment

ahhh I completely missed that. Cheers for that
0

The
System.Data.DataSet MyDataSet() is not what you have to put in your code if you are using Mysql

first initialize the code. then write methods to open and close connection. now you are ready for the db connection.

    public class DbConnection
    {
    private MySqlConnection connection;
    private string server;
    private string database;
    private string uid;
    private string password;

    private void initialize()
    {
        server = "localhost";
        database = "yourdatabase";
        uid = "root";
        password = "";
        string connectionString = "server=" + server + ";database=" + database + ";uid=" + uid + ";password=" + password + ";";

        connection = new MySqlConnection(connectionString);
    }

    //open connection

    private bool openConnection()
    {

        try
        {
            connection.Open();
            return true;
        }
        catch(MySqlException ex)
        {
            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;
        }



    }

if you want to develop complete db connection use this site: http://www.codeproject.com/Articles/43438/Connect-C-to-MySQL helped me a lot :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.