tags:

views:

182

answers:

8

Hello.Im a beginner in SQL.I am having a problem with it. First of all I shd let u know I installed SQL 2005 Server n then made a ConsoleApp and rest of code is under :

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection myConnection = new SqlConnection("user id=userid;" +
                                       "Password=validpassword;" +
                                       "Trusted_Connection=yes;" +
                                       "Data Source=localhost;" +
                                       "connection timeout=300");



            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }


            SqlCommand myCommand = new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                      "Values ('string', 1)", myConnection);

            myCommand.ExecuteNonQuery();



        }
    }
}

WHEN I RUN IT...I get An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll with myCommand.ExecuteNonQuery();

Kidnly guide me where Im going wrong.I just want to do simple stuff.Insert something n then read it.But I get error even at inserting.

A: 

There is either an issue with the connection to the database- wrong username, password, and/or server, or the table and columns do not exist. I would place a breakpoint on the catch statement and take a look at the exception to find out more.

Shawn Simon
+6  A: 

You aren't using a database. Set the Initial Catalog query in your connection string to the database that the table "table" is in. Or, do INSERT INTO dbName..table VALUES ...

Also, check out ConnectionStrings.com. Learn it, use it, love it.

Eric
Excellent catch! No "Initial Catalog=" or "database=" in the connection string - where does that "table" exist then? :-)
marc_s
+3  A: 

You appear to be missing the Initial Catalog parameter in your connection. It should be "Initial Catalog = DatabaseName". Also if you are using Trusted Connection you don't need the user id and password parameters.

Ryan
+2  A: 

Start my moving your exception handling to contain the ExecuteNonQuery, then edit your question with the full exception.

        static void Main(string[] args)
        {
            SqlConnection myConnection = new SqlConnection("user id=userid;" +
                                       "Password=validpassword;" +
                                       "Trusted_Connection=yes;" +
                                       "Data Source=localhost;" +
                                       "connection timeout=300");


            try
            {
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                      "Values ('string', 1)", myConnection);
                myCommand.ExecuteNonQuery();    
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
bendewey
Perhaps use a Using statement too?
Dead account
A: 

If you trap the exception, or debug the console app so that you can see the exception details, you should be able to find out the full details of the error by examining the message of the exception, or the inner exception.

As Chad mentioned, this could be a simple naming problem where the table or columns you specified in the query don't exist in the database you are pointed to. Alternatively, there could be an issue with the type of data you are trying to interpret. The DB Server message that comes back on the exception will let you know more.

Jay S
+1  A: 

When you installed SQL Server 2005 did you put it into mixed mode? You would need this to pass in a username and password. I am not sure what .NET will do with a username/password and trusted connection = true in the connection string. Are you getting an open connection?

You also need a database name in your connection string.

I can't add a commet on your post so I will add it here. You need to start up the SQL Server Management Studio to get to your database and future tables. Then go to help and begin reading books online about how to create your tables.

hipplar
Thanks Im doing it
A: 

YES THERE ISNT ANY TABLE IN MY DATABASE.ACTUALLY I DONT EVEN KNOW HOW TO ACCESS THE DATABASE AND MAKE TABLE IN IT...:(

IT WILL BE BETTER IF YOU GUYS JUST HOOK ME WITH A LINK TO ANY TUTORIAL WHERE I CAN LEARN THIS BASIC STUFF

easy there cowboy why the screaming?
dotjoe
Ah, there's the issue. You can't write to a database and/or table that doesn't exist, mate.You'll want to open up SQL Server Management Studio and add a database pronto. Then you can make your table with columns and all the trimmings.A quick Google for database basics gave me this: http://www.geekgirls.com/menu_databases.htm -- at first glance, it looks like it'll get you started. Use MSDN, as well. Databases are rock hard awesome, and I applaud you for learning about them. Enjoy!
Eric
A: 

Oki I have made a table with 2 columns but now in Intial Catalog shd I give the whole path to the table or wht exactly do u mean by database name ?

Thats where it is:

My Server\Databases\System Database

Wht shoud I write in Initial Catalog= then???

There are some excellent books from Wrox, Apress and others that are specifically oriented towards beginner programmers in c# and sql server 2005/2008. I suggest you grab one and read it from cover to cover. That's pretty much what I did at the beginning of my software engineering career.
Terry Donaghe
Thank you very much