0

I'm trying to connect to a local SQL Server database file and do not know if connection string is right:

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ma\Documents\mydb.mdf;Integrated Security=True;");
SqlDataAdapter sda = new SqlDataAdapter("SELECT plataform FROM plataforms", con);

DataSet myDataSet = new DataSet();
sda.Fill(myDataSet);

I have such code wrapped in a try catch and always throws this exception:

Reference to object not established as an instance of an object

What's wrong?

EDIT:

Sorry, I have been commenting code to see what line arises such error and it's the following:

DataRowCollection drc = myDataSet.Tables["plataforms"].Rows;

Sorry, I made a wrong question.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Apalabrados
  • 1,098
  • 8
  • 21
  • 38

1 Answers1

0

I think You must open connection before Fill

SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\ma\Documents\mydb.mdf;Integrated Security=True;");
SqlDataAdapter sda = new SqlDataAdapter("SELECT plataform FROM plataforms", con);

SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = sda;
DataSet myDataSet = new DataSet();
try {
      con.Open();
      sda.Fill(myDataSet);
   } catch (Exception ex) {
      throw (ex);
   } finally {
      con.Close();
   }

you can try this code.

Jaycey
  • 18
  • 3