Newbie Visual Studio / SQL Question

telstar1

Golden Member
Feb 14, 2001
1,206
0
0
I'm tinkering around with Visual Studio .NET and I'm having problems getting basic database connectivity working.
I've got the SQL Server 2000 Desktop Engine up and running, and I've got a C# program that's trying to connect.
My code looks like this:


InitializeComponent();
string connectionString = "server=localhost; uid=sa; pwd=; database=master";
string commandString = "Select EmployeeName, ID from Employees";

SqlDataAdapter DataAdapter = new SqlDataAdapter(commandString, connectionString);
DataSet DataSet = new DataSet();
DataAdapter.Fill(DataSet,"Employees");
DataTable dataTable = DataSet.Tables[0];



The problem is that my program seems to fail on the bold line with the following error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.


Has anybody experienced anything like this, or do you have any suggestions on how to resolve this connectivity problem?

Thanks,
Telstar




 

joohang

Lifer
Oct 22, 2000
12,340
1
0
Two suggestions:
1) I don't recommend naming an object after its object type (i.e. your DataSet).
2) Add a try/catch block.

try {
// Data connection and all that stuff go here
}
catch(SqlException ex)
{
return ex.Message.ToString();
}

*note: I am not sure whether SqlException is the correct exception handler. You might wanna look it up if it returns an error.

<-- too used to IntelliSense :eek: :)
 

manly

Lifer
Jan 25, 2000
13,086
3,850
136
As I've mentioned before, it's sheer folly that all exceptions in the CLS (aka .Net languages) are unchecked.

Most exceptions should be part of the method call signature, and handling them enforced by the language.

Now whether a programmer handles exceptions smartly is another issue entirely.

But the stupid decision has been made, and there's no way to change it now without breaking legacy code. In other words, it ain't gonna happen.
 

joohang

Lifer
Oct 22, 2000
12,340
1
0
Edit my other post. I don't think you need the ToString() in the Message. It's already a string.
 

telstar1

Golden Member
Feb 14, 2001
1,206
0
0
Got it working. Aside from the poorly named variables (copied verbatum from the book, and detailed as errata here) I ended up playing with the database settings for a number of hours, and installed the SQL2000 client tools.

Thanks for the suggestions.