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