I am having trouble trying to send my data into my database.
Basically, I am using an ASP.Net Web form already implemented by Visual Studio 2012 (Professional). I have created an extra page called 'users.asps' with only three text boxes (username, full name and email). Then I also created a new database called 'Datatest.mdf' and added ONE table in the database called 'users' which includes three attributes: username (primary key), name and email.
Below is my code for the button on the page. Here is where I want to send all the inputted data into my database table.
protected void Button1_Click(object sender, EventArgs e)
{
string strun = Request.Form["username"];
string strna = Request.Form["name"];
string strem = Request.Form["email"];
OleDbConnection objconnection = null;
OleDbCommand objcmd = null;
string stringconnection = null;
string strSQL = null;
//connection string
stringconnection = "provider=SQLOLEDB;data source=(LocalDb)\\v11.0;Initial Catalog=Datatest; Integrated Security=SSPI;";
objconnection = new OleDbConnection(stringconnection);
objconnection.ConnectionString = stringconnection;
objconnection.Open();
strSQL = "insert into users(username, name, email)values(?,?,?)";
objcmd = new OleDbCommand(strSQL, objconnection);
objcmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@username", strun));
objcmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@name", strna));
objcmd.Parameters.Add(new System.Data.OleDb.OleDbParameter("@email", strem));
objcmd.ExecuteNonQuery();
//close connection
objconnection.Close();
Response.Write("Entered Successfully");
}
When I run the form however I am getting an error "OleDbException was unhandled by user code" then says "SQL Server does not exist or access denied" that has an arrow pointing at objconnection.Open();
.
Despite where the error is located, I know the problem lies where my string connection is, I just don't know how I can fix it.
stringconnection = "provider=SQLOLEDB;data source=(LocalDb)\\v11.0;Initial Catalog=Datatest; Integrated Security=SSPI;";
For all your connectionstring propblems. You want to take a look in the sql department of that website.
I think that SQLOLEDB provider doesn't understand the connection string required to use a LocalDB.
(The LocalDB is more recent than SQLOLEDB)
Try to use SQLNCLI11 (The Sql Server Native Client v11)
"provider=SQLNCLI11;data source=(LocalDb)\\v11.0;
Initial Catalog=Datatest; Integrated Security=SSPI;";
However, if you are in the initial stage of developping your application I really suggest to use the SqlClient classes like SqlConnection, SqlCommand etc and leave behind the OleDb and all its limitations. (Like no support for named parameters)
using System.Data.SqlClient;
......
stringconnection = @"Data Source=(LocalDb)\\v11.0;Initial Catalog=Datatest;
Integrated Security=SSPI;";
using(objconnection = new SqlConnection(stringconnection))
{
objconnection.Open();
strSQL = "insert into users(username, name, email)values(@username, @name, @email)";
using(objcmd = new SqlCommand(strSQL, objconnection))
{
objcmd.Parameters.Add(new SqlParameter("@username", strun));
objcmd.Parameters.Add(new SqlParameter("@name", strna));
objcmd.Parameters.Add(new SqlParameter("@email", strem));
objcmd.ExecuteNonQuery();
}
}
Response.Write("Entered Successfully");
Try to visit this link https://www.daniweb.com/software-development/csharp/threads/339949/how-to-establish-a-connection-with-sql-server-using-c
maybe it can help you on your database connection problem :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.