0

I tried to create a localdb with visual studio. I use c# for connecting and inserting a new row. I do not understand the problem is in visual studio server explorer database connection or in my code.

To insert row to db, I get an idea from [Using SqlDataAdapter to insert a row . When user click button this connection should be working but I get null reference exception while debugging

SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB;AttachDbFilename= C:\somepath.mdf; Integrated Security = True;");
con.Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("SELECT * FROM mytablename WHERE 0 = 1", con);

DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet);

DataRow newRow = dataSet.Tables["mytablename"].NewRow();  // null reference exception
newRow["username"] = "un";
newRow["myaccountusername"] = "maun";
dataSet.Tables["mytablename"].Rows.Add(newRow);

new SqlCommandBuilder(sqlDataAdapter);
sqlDataAdapter.Update(dataSet);

I created 3 localdb in visual studio so only one of them has connected. Maybe I am working with the wrong one. If there is a problem in variable con, how can I understand? To realise a problem easily, the values of con while debugging are in below;

AccessToken null    string
CanRaiseEvents  true    bool
+       ClientConnectionId  {1a523d10-43d8-48ed-a5b8-d6a909507f1e}  System.Guid
        ConnectionString    "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename= C:\\Users\\somepath.mdf; Integrated Security = True;" string
        ConnectionTimeout   15  int
        Container   null    System.ComponentModel.IContainer
        Credential  null    System.Data.SqlClient.SqlCredential
        DataSource  "(LocalDB)\\MSSQLLocalDB"   string
        Database    "C:\\USERS\\somepath.MDF"   string
+       DbProviderFactory   {System.Data.SqlClient.SqlClientFactory}    System.Data.Common.DbProviderFactory {System.Data.SqlClient.SqlClientFactory}
        DesignMode  false   bool
+       Events  {System.ComponentModel.EventHandlerList}    System.ComponentModel.EventHandlerList
        FireInfoMessageEventOnUserErrors    false   bool
        PacketSize  8000    int
        ServerVersion   "12.00.2000"    string
        Site    null    System.ComponentModel.ISite
        State   Open    System.Data.ConnectionState
        StatisticsEnabled   false   bool
        WorkstationId   "ASUSN56"   string
+       Static members      
+       Non-Public members      
jazb
  • 5,498
  • 6
  • 37
  • 44
ozan
  • 33
  • 1
  • 9
  • The first think is to fix the connection string and remove the mdf filename. It is not needed. The database knows the location of the file when it is attached. I usually put the database name in the command text select query by adding "USE MyDatabase Select .....". Make sure the dataset contains data. I normally put break point after dataset is filled. The hover over ds and click on down arrow and select Dataset Visualizer. There is a text box the allows selection of each table. – jdweng Dec 17 '18 at 05:39
  • Connection string looks filthy and also no null check anywhere? – Navoneel Talukdar Dec 20 '18 at 12:43

0 Answers0