0

I connected server I have error in adapter.fill(dt) line

        {
            DataTable dt = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter("select * from musteriii",a);
            adapter.Fill(dt);
            Console.WriteLine(dt.ToString());
            Console.ReadLine();
        }

I am learning how can I use SqlDataAdapter but I have a problem please help me.

Dale K
  • 25,246
  • 15
  • 42
  • 71
taha Glbz
  • 1
  • 1
  • 1
    Hello and welcome to Stack Overflow. Please read the rules on how to ask a good question: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). In particular: Describe exactly what you want to achieve, what you did and what errors (exceptions or similar) you got under what conditions these errors occur. – deralbert Aug 13 '23 at 12:22
  • Does ['instance failure' error while connection string is correct](https://stackoverflow.com/questions/17650482/instance-failure-error-while-connection-string-is-correct) help? – Andrew Morton Aug 13 '23 at 12:27
  • Are you sure that the `SqlConnection` was already open before entering this block of code? These errors are typically thrown at connection open time because there's something incorrect about the properties in the connection string. – AlwaysLearning Aug 13 '23 at 12:28
  • SqlConnection a = new SqlConnection(@"Data Source=DESKTOP-*******\\SQLEXPRESS;Initial Catalog=SQLFULL;Integrated Security=True"); I wrote this line and my sqql connection property true – taha Glbz Aug 13 '23 at 12:38
  • I restarted sql server services but fail already continue – taha Glbz Aug 13 '23 at 12:43
  • When using verbatim strings, `@"..."`, you don't need to escape your backslashes. Have you tried using just a single backslash here? Ref: [Verbatim text - @ in variables, attributes, and string literals](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim). – AlwaysLearning Aug 13 '23 at 13:05
  • Please give debugging details, see [ask]. – JHBonarius Aug 13 '23 at 14:21
  • @AlwaysLearning The [`DbDataAdapter.Fill Method`](https://learn.microsoft.com/en-us/dotnet/api/system.data.common.dbdataadapter.fill) does need an open connection. It will take care of that itself. – Andrew Morton Aug 14 '23 at 06:55
  • Thank you, @AndrewMorton, I'm aware of that. I was challenging Op's assertion that they had already established the SQL Server connection because the exception message was indicating otherwise. – AlwaysLearning Aug 14 '23 at 07:59
  • @AlwaysLearning Of course, I missed out the vital "not", as in "does not", in that comment. Oops. – Andrew Morton Aug 14 '23 at 14:30
  • @tahaGlbz If the database is on the same machine as the program, then `@"Data Source=.\SQLEXPRESS;Initial Catalog=SQLFULL;Integrated Security=True"` is more likely to work. – Andrew Morton Aug 14 '23 at 14:32

1 Answers1

0

Can you try it like this? SqlConnection a = new SqlConnection(@"Data Source=DESKTOP-*******\SQLEXPRESS;Initial Catalog=SQLFULL;Integrated Security=True");

And is your SQLConnection open?

gkmnada
  • 64
  • 6