connection string data source error

0

in an aspx.vb file

Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim con As New OleDbConnection("Provider= SQLOLEDB.1;Integrated Security=true; data source=myserver; ")

        Dim cmd As New OleDbCommand

        cmd.Connection = con
        cmd.CommandText = "Insert into demo.dbo.masters1 values ('" & TextBox1.Text & " ," & TextBox4.Text & " , " & TextBox3.Text & " ')"
        con.Open()
        If (cmd.ExecuteNonQuery() > 0) Then
            Label1.BorderColor = Drawing.Color.Aquamarine
        Else
            Label1.BorderColor = Drawing.Color.Black
        End If

        con.Close()

End Sub

I am using Windows Authentication.

The server is up and running.

I've also tried:

Dim con As New OleDbConnection("Provider= SQLOLEDB.1;Integrated Security=true; data source=C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\database_name.mdf; ")

However, I'm getting the following error:

No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21).

asp.net
sql-server
vb.net
asked on Stack Overflow Jan 6, 2016 by marquis • edited Jan 6, 2016 by Hannah Vernon

1 Answer

1

Look like you are using OleDbConnection for SQL Server.

You want to SqlConnection if you use SQL Server. For example,

using(var con = new SqlConnection(
     "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"))
{
   var query = "insert statement ...";
   using(var cmd = new SqlCommand(query, con)){
      cmd.CommandType = CommandType.Text;
      con.Open();
      ....
   }
}

You might want to look at the connection string format here too.

FYI: Use the parameterize query to avoid SQL Injection attack.

answered on Stack Overflow Jan 6, 2016 by Win

User contributions licensed under CC BY-SA 3.0