Im still in the learning stages and have spent the majority of today searching around trying to follow tutorials or get information on how to simply use sql (i know the sql 'language' as have worked with databases a long time via php,vba and access but c# is new to me and not as simple to conenct to (althoguh will be once i understand how to!)
The Error i get is: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.
my code is
{
private void button1_Click(object sender, System.EventArgs e)string istring = @"INSERT INTO tblUsers(username, distance) VALUES('Test',54321);";SqlConnection conn =
conn.Open();
new SqlConnection(@"Data Source=L:\testing\in the distance\ITD.mdb");
SqlCommand cmd =
new SqlCommand(istring, conn);// this is where it debugscmd.ExecuteNonQuery();
if (conn != null)
{
conn.Close();
}
}
Thanx
Loading

VulpesRank #55 · 29.2K reputationPosted Jul 20, 2011, 7:37 AM EDT
SqlConnection is for SQL Server. For Access you need to use OleDbConnection and change the rest of the code to use this provider.
So, try:
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=L:\testing\in the distance\ITD.mdb;User Id=admin;Password=;");
and change SqlCommand to OleDbCommand.
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 20, 2011, 7:04 AM EDT
Try the same statement but only limiting the value to something smaller, e.g. this:
string istring = @"INSERT INTO tblUsers(username, distance) VALUES('Test',100);";
Zoran
Box51 reputationPosted Jul 20, 2011, 7:09 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jul 20, 2011, 7:22 AM EDT
If you're using MS Access, I'd try this instead:
SqlConnection conn = new SqlConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=L:\testing\in the distance\ITD.mdb; User Id=admin; Password=;");
Box51 reputationPosted Jul 20, 2011, 7:26 AM EDT
different error now :(
An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll
Additional information: Keyword not supported: 'provider'.
VulpesRank #55 · 29.2K reputationPosted Jul 20, 2011, 7:56 AM EDT
In your 'using' directives, you'll need to change this line:
using System.Data.SqlClient;
to this:
using System.Data.OleDb;
so the compiler will be able to find the correct classes.
Jiteendra SampathiraoRank #585 · 4.1K reputationPosted Jul 20, 2011, 9:01 AM EDT
new SqlConnection(@"Data Source=L:\testing\in the distance\ITD.mdb");
place it under app_data and right click on mdb file and copy the file address...
use that as connectionstring....
sqlconnection sqlconn=new sqlconnection(fileaddress).........
use this directive System.Data.Sqlclient....or System.Data.Oledb;
but check if mdb file has any access rights........
Sam HobbsRank #107 · 20.5K reputationPosted Jul 20, 2011, 7:55 PM EDT
Abhimanyu K VatsaRank #14 · 51.7K reputationPosted Jul 21, 2011, 12:02 AM EDT
Box51 reputationPosted Jul 21, 2011, 3:50 AM EDT
This is the code now (at the bottom)
Sam, If there are better ways of doing things I am very interested though. I am just learning at the moment so want to avaoid bad habbits early on ideally.
Basicly im just trying to manupulate data in a table at the moment.
private void button1_Click(object sender, System.EventArgs e)
{
string istring = @"INSERT INTO tblUsers(username, distance) VALUES('Test',121);";
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=L:\BDSU\Archive\Personal - TO BE SORTED\Scott Thurlow\InTheDistance.mdb; User Id=admin; Password=;");
conn.Open();
OleDbCommand cmd = new OleDbCommand(istring, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
Sam HobbsRank #107 · 20.5K reputationPosted Jul 21, 2011, 9:15 AM EDT
It is possible to create a data source in VS that will generate code that makes it easier to manipulate data in a table. It does require some learning but when you know how to do it the better way, you won't need to waste time with details. There are basically two ways to create a data source; either TableAdapters or Entities.
Have a look at my Database Table Update in a DataGridView without Writing Code; it describes creation of a TableAdapter data source. It does not describe how to use TableAdapters for manipulating data but TableAdapters can be useful for that.
There are other articles in this web site about Entities. If the articles are not enough, then it will help to get a book about database programming.
It is useful for you to learn about SqlConnection and the other ADO classes. Just understand that you don't need to be concerned about detailed classes such as that if you create a data source and manipulate the data with it.