-1

i am having following trouble with this code.

unhandled exception of type 'System.ArgumentException' occurred in System.Data.Dll

static void Main(string[] args)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
        SqlCommand cmd = new SqlCommand("Select * from Student", con);
        con.Open();
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Console.WriteLine("{0}", dr[0].ToString());
        }
        Console.ReadKey();
    }
samEE666
  • 3
  • 6

3 Answers3

2

Problem: You have single quote &quot before and after the Database filename .
Solution : you don't need to provide single quote &quot for database file name.so remove the &quot before and after the database filename.

Try This:

con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";

EDIT :

if your table name is Table you should enclose it in square brackets [] as it is a Reserved word in SQL-Server.

Try This:

 SqlCommand cmd =  new SqlCommand("Select * from [Table]",con );

Solution 3: you need to use while loop to display all values.

static void Main(string[] args)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlCommand cmd =  new SqlCommand("Select * from [Table]",con );
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine("{0}",dr[0].ToString());
}
Console.ReadKey(); 
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • Exception is handled but I'm not getting output yet is there any other problem?? – samEE666 Dec 11 '13 at 05:23
  • @samEE666: is your table name is really `Table`? – Sudhakar Tillapudi Dec 11 '13 at 06:17
  • @samEE666 : Table is a reserved word in sql server , just enclose it in square brackets, see my edited answer. – Sudhakar Tillapudi Dec 11 '13 at 07:13
  • i had also changed the Table and added a new table naming Student ... when i select show table data there is data but when i run the program there is no output .... – samEE666 Dec 11 '13 at 07:36
  • @samEE666: you need to use while lopp to display all rows, see my edited answer with `solution 3`. – Sudhakar Tillapudi Dec 11 '13 at 11:37
  • @samEE666: could you please edit your question with latest code you are trying so that i can help much better :) – Sudhakar Tillapudi Dec 11 '13 at 12:44
  • @samEE666: could you please debug it and see where it is throwing the error? – Sudhakar Tillapudi Dec 11 '13 at 13:04
  • Exception occurred when control reached at `con.Open();` – samEE666 Dec 11 '13 at 13:11
  • A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) – samEE666 Dec 11 '13 at 13:11
  • @samEE666: ok this is because there is a problem with the connection string , check wether MDF file exists or not in the given path. – Sudhakar Tillapudi Dec 11 '13 at 13:14
  • @ Sudhakar Tillapudi: i changed the Connection String with this and it worked thanx man .. `con.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\MUHAMMAD\Documents\samEE.mdf;Integrated Security=True;Connect Timeout=30";` – samEE666 Dec 11 '13 at 13:19
  • @samEE666: Excellent :) , glad to see your problem got resolved :) – Sudhakar Tillapudi Dec 11 '13 at 13:22
1

replace &quote with an escaped quote \" in your connection string so that it's

        con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=\";C:\USERS\MUHAMMAD\DOCUMENTS\SAMEE.MDF\";Integrated Security=True;Connect Timeout=30;User Instance=True";

The string, as you have it, would only work if it was in a .config file. (The .config files are XML, and &quote is an XML-Encoded representation of a quotation mark.)

David
  • 72,686
  • 18
  • 132
  • 173
0

you can see this exampl.

but i think that your problem is that you open the connection but you never close connection;

//this is my class  of data or my entity 
public class datos 
    {
        public string column1 { get; set; }
        public string column2 { get; set; }
    }     

   //create a string with the connections parameter
   public static string  myConnection { get { return       @"Server=.\SQLExpress;AttachDbFilename=C:\Users\base.mdf;Trusted_Connection=Yes;"; } }

    //Create a method  of type List<datos> to return a list of datos
public List<datos> example (){   
List<datos> lista = new List<datos>();
      //declare and initialize my entity of type datos
        datos dat = new datos();
        //create a new command to do a query to my database
        SqlCommand adaptador = new SqlCommand("Select * from Yourtable", myConnection);
         //open my connection 
         myConnection.Open();
        // execute my command
         SqlDataReader x =  adaptador.ExecuteReader();
        //now i read the data that i get from my command and add data to my list
         while (x.Read()) 
         {
            dat.column1 = x["column1fromyoutable"].ToString();
            dat.column2 = x["column2fromyourtable"].ToString();
            lista.Add(dat);
         }
        // close connection
         myConnection.Close();
        //return list of data
        return lista;
    }
Ricardo Romo
  • 1,588
  • 12
  • 25