Click to See Complete Forum and Search --> : OBBC connection


vicky94804
January 3rd, 2003, 05:28 PM
Hello,

When I run my web application which is created using ASP.net and C#. I count error message "ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified". But When I run it using wondow form , it works.
I really don't know what cause this problem? Can anyone help me?

Thanks,
Vicky

Leo Ayala
January 18th, 2003, 04:21 PM
I myself have spent a lot of time trying to figure this out. I recently found out some information and I got it to work for me. I hope it helps you if you still have not found a solution.

First of all, you cannot use DSN names. You must specify all connection information. For example:

string ConStr = "Driver={Microsoft Visual FoxPro Driver}; Database=CUSTOMER; SourceType=DBF; SourceDB=C:\\Databases; UID=Leo; PWD=;"

Where:
Driver = The database driver you are using to connect to the database. In my case, the Microsoft Visual FoxPro Driver works for opening DBF (DBase IV) databases. You can get the name of the driver from the ODBC Data Source Administrator in the control panel.

Database = The name of the database you are trying to connect to. I did not have to specify the database extension.

SourceType = The type of database. In my case, this is a DBF-type.

SourceDB = The directory where the database is located.

UID/PWD = The user ID and password. Although I don't think it is required. You can experiment with or without it.

Second, sometime during the installation of .NET and the Framework, a user account of ASPNET was created. I think this is the default user that .NET uses when debuging ASP.NET apps unless you specify a differnet user (I'm not sure how this is configured). However, the user must be a member of the 'Debugger Users' group. You can add membership to this group in the control panel. If you use your own user id, then you must be a member of this group as well.

Third, you must have the Microsoft ODBC Data Provider. This was not included with .Net. You can download it from:

http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/001/668/msdncompositedoc.xml

Below is some sample code that has worked for me. Note that although I am using the Microsoft Visual FoxPro driver, my database is not a Visual FoxPro database. It is a DBase IV type database (*.DBF). This drive just happens to work for me.

I have a web form with a button and a listbox. When the button is clicked, it fills the listbox with items from one of the columns in the database. 'Output' is a label that displays any errors.


private void Button1_Click(object sender, System.EventArgs e)
{
string ConStr = "Driver={Microsoft Visual FoxPro Driver};Database=SYSTEMCODE;UID=ASPNET;PWD=;SourceType=DBF;SourceDB=C:\\Approach";

OdbcConnection OdbcConn = new OdbcConnection(ConStr);

try
{
OdbcConn.Open();
string SQL = "SELECT * FROM SYSTEMCODE";

OdbcCommand OdbcComm = new OdbcCommand(SQL, OdbcConn);
OdbcDataReader OdbcReader = OdbcComm.ExecuteReader();

while(OdbcReader.Read())
{
ListBox1.Items.Add(OdbcReader.GetString(1));
}
}
catch(Exception ex)
{
Output.Text = ex.Message;
}
finally
{
OdbcConn.Close();
}
}

I hope this helps.

-LA