9 Replies - 2007 Views - Last Post: 25 July 2011 - 02:01 AM Rate Topic: -----

#1 xavi8808   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-October 10

Exception/Error inserting into the database

Posted 22 July 2011 - 02:11 AM

I am trying to insert into the database but i am getting an exception that the object i.e the table
does not exist could anyone have an ideas on how i can go over this? My connection string returns open when i check.
below is my code

/*Defining the connection string*/
                string connectionString = @"Data Source=.\SQLEXPRESS;"+
                                          @"AttachDbFilename= D:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Template Data\master.mdf;" +
                                          @"Initial Catalog=BookShopDB;Integrated Security=True;Connect Timeout=100;User Instance=True";
                    /*Defining the connection*/
                SqlConnection conn = new SqlConnection(connectionString);
                try
                {
                    conn.Open();
                    MessageBox.Show(conn.State.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Sorry" + ex.Message);
                
                }

                try
                {
                    /*Define the command string */
                string cmdString = "INSERT INTO student VALUES ('tome','Haze','2000','2344xr');";
                SqlCommand insertCommand = new SqlCommand(cmdString,conn);
                
                    //insertCommand.CommandType = CommandType.Text;

                    insertCommand.ExecuteNonQuery();
                }
                catch(Exception EX){
                MessageBox.Show("Sorry" + EX.Message);

                }




Is This A Good Question/Topic? 0
  • +

Replies To: Exception/Error inserting into the database

#2 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Exception/Error inserting into the database

Posted 22 July 2011 - 03:46 AM

Are you 100% sure that the table 'student' exists in the database 'master.mdf'?

Also post the full exception here.
Was This Post Helpful? 0
  • +
  • -

#3 xavi8808   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-October 10

Re: Exception/Error inserting into the database

Posted 22 July 2011 - 04:10 AM

I think am connecting to a wrong database.I have looked at Master.mdf and when i insert values go
student table is in BookShopDB database.
I have a problem in building the connection string.
Could you show me how this string is built.
Thanks
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Exception/Error inserting into the database

Posted 22 July 2011 - 07:05 AM

SQL Express is a service-based database. You don't normally attach the actual dbf file. You can, but if you are going to attach the database file, you might want to look into using SQLCE instead.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Exception/Error inserting into the database

Posted 22 July 2011 - 07:30 AM

Actually, it's not such a bad thing to attach the DB file, but usually people use the DataDirectory functionality to simplify. The reason a lot of people do this is because it's built into VS to add a DB to the project.
Was This Post Helpful? 0
  • +
  • -

#6 xavi8808   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-October 10

Re: Exception/Error inserting into the database

Posted 23 July 2011 - 03:48 AM

Thank you everyone.But i have not yet found the solution..
I commented out the line that attach the DB file
i.e
@"AttachDbFilename= D:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Template Data\master.mdf;"


but still my student table object can not be seen.
If anyone could show me how to build up a connection string from Visual c# express edition or better still build the string manually.That would great
Xv
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Exception/Error inserting into the database

Posted 23 July 2011 - 06:18 AM

View PostCurtis Rutland, on 22 July 2011 - 10:30 AM, said:

Actually, it's not such a bad thing to attach the DB file, but usually people use the DataDirectory functionality to simplify. The reason a lot of people do this is because it's built into VS to add a DB to the project.


But to attach a SQL Express database? I can see if it was a SQLCE database, but SQL Express wasn't really designed to be attached in a connection string like that. It's meant to run as a service and connected to.
Was This Post Helpful? 1
  • +
  • -

#8 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Exception/Error inserting into the database

Posted 23 July 2011 - 07:43 AM

View Postxavi8808, on 23 July 2011 - 06:48 AM, said:

Thank you everyone.But i have not yet found the solution..
I commented out the line that attach the DB file
i.e
@"AttachDbFilename= D:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Template Data\master.mdf;"


but still my student table object can not be seen.
If anyone could show me how to build up a connection string from Visual c# express edition or better still build the string manually.That would great
Xv


Ok, we're going around in circles here. Let's clarify a few things.

A ) Are you correctly attaching the database? If so, how are you verifying this?

B ) You say you can't see the Student table; does this mean you can see the other tables?
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Exception/Error inserting into the database

Posted 23 July 2011 - 11:43 AM

Many ASP.NET applications do that with their aspnet db file. Actually, that's what VS will do for you automatically, if you use the site configuration tool to set up forms authentication. You won't even see the DB in your project, but if you browse to it on the file system, it'll be there.

Also, I've never heard of attaching a Sql Compact file. Why attach something that's fully standalone? The idea behind auto-attach is so that you can build your application entirely as a package, then deploy it to a server as a package. You don't need remote access to the server's DB to develop.
Was This Post Helpful? 0
  • +
  • -

#10 xavi8808   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 13-October 10

Re: Exception/Error inserting into the database

Posted 25 July 2011 - 02:01 AM

View PostSergio Tapia, on 23 July 2011 - 07:43 AM, said:

View Postxavi8808, on 23 July 2011 - 06:48 AM, said:

Thank you everyone.But i have not yet found the solution..
I commented out the line that attach the DB file
i.e
@"AttachDbFilename= D:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Template Data\master.mdf;"


but still my student table object can not be seen.
If anyone could show me how to build up a connection string from Visual c# express edition or better still build the string manually.That would great
Xv


Ok, we're going around in circles here. Let's clarify a few things.

A ) Are you correctly attaching the database? If so, how are you verifying this?

B ) You say you can't see the Student table; does this mean you can see the other tables?


First i think i was connecting to a wrong database. I used the Add data source option that is in Visual c# express edition and i followed the dialog/ wizard and browsed for the database, There is the advanced button which allow you to add the database name you are connecting to and i put the BookShopDB that i want to use.

I can insert into masters database's table that is a system database that come with MS SQL EXPRESS EDITION and that is how i know i am connection to a wrong database.

Would you tell me what i am supposed to fill for each parameter in the connection string and what each is supposed to mean.

@"Data Source=............."+
@"AttachDbFilename= .......................;" +
@"Initial Catalog=.........;Integrated Security=.......;Connect Timeout=........;User Instance=.....";


I am trying to connect and use a database called BookShopDB, that i made using MS SQL SERVER R2 and the instance am using is SQLEXPRESS.
Thank you
XV
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1