0

Please forgive me if I am overlooking something really obvious here, as I am totally new to programming. I have spent the last couple of days trying to troubleshoot an issue I have been having. Long story short, I have a CRM app that I am building that includes a database with four tables (Employee, Responses, Status and Table). I believe that my issue is with my connection string, but I am unsure of how to fix it. Per the database properties window, the connection string is as follows:

Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf";Integrated Security=True

I am currently receiving the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in 349591.exe

Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: An expression of non-boolean type specified in a context where a condition is expected, near ','.

I have tried everything I know, and am stumped. Thank you for any help you can provide. Please find the DBUtil code I wrote below:

Imports System.Data
Imports System.Data.SqlClient

Public Class DButil
    Public cs As String

    Public Function GetDataView(ByVal sql As String) As DataView
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(sql, cs)

        da.Fill(ds)

        Dim dv As New DataView(ds.Tables(0))
        Return dv
    End Function

    Public Sub New()
        'cs = "Data Source=(LocalDB)\v11.0;"
        'cs = "Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True"
        'cs += "Integrated Security =True;"

        ' Dim strPath As String = Replace(System.AppDomain.CurrentDomain.BaseDirectory, "bin\debug\", "cms.mdf")
        cs = ("Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True")
    End Sub

    Public Function SaveComplaint(
        ByVal ComplaintID As Integer,
        ByVal Description As String,
        ByVal Proposal As String,
        ByVal Location As String,
        ByVal OpenDate As Object,
        ByVal CloseDate As Object,
        ByVal StatusID As Integer,
        ByVal EmployeeID As Integer) As Boolean

        OpenDate = Convert.ToDateTime(OpenDate)

        If CloseDate.ToString.Length = 0 Then
            CloseDate = DBNull.Value
        Else
            CloseDate = Convert.ToDateTime(CloseDate)
        End If

        Dim sql As String

        If ComplaintID = 0 Then
            sql = "INSERT INTO Complaints (Description, Proposal, Location, OpenDate, "
            sql += "CloseDate, StatusID, EmployeeID) VALUES (@Description, @Proposal, "
            sql += "@Location, @OpenDate, @CloseDate, @StatusID, @EmployeeID)"
        Else
            sql = "UPDATE Complaints SET Description=@Description, Proposal=@Proposal, Location=@Location, OpenDate=@OpenDate, "
            sql += "CloseDate=@CloseDate, StatusID=@StatusID, EmployeeID=@EmployeeID WHERE ComplaintID=@ComplaintID"
        End If

        Dim cn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename='C:\Users\Sean\Documents\Visual Studio 2013\Projects\349591\349591\cms.mdf';Integrated Security=True")
        Dim cm As New SqlCommand(sql, cn)

        Try
            With cm.Parameters
                .AddWithValue("@ComplaintID", ComplaintID).DbType = DbType.Int32
                .AddWithValue("@Description", Description).DbType = DbType.String
                .AddWithValue("@Proposal", Proposal).DbType = DbType.String
                .AddWithValue("@Location", Location).DbType = DbType.String
                .AddWithValue("@OpenDate", OpenDate).DbType = DbType.DateTime
                .AddWithValue("@CloseDate", CloseDate).DbType = DbType.DateTime
                .AddWithValue("@StatusId", StatusID).DbType = DbType.Int32
                .AddWithValue("@EmployeeID", EmployeeID).DbType = DbType.Int32
            End With

            cn.Open()
            cm.ExecuteNonQuery()
            cn.Close()

            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error in DBUtil.SaveComplaint",
                MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function RunSQL(ByVal sql As String) As Boolean
        Dim cn As New SqlConnection(cs)
        Dim cm As New SqlCommand(sql, cn)

        Try
            cn.Open()
            cm.ExecuteNonQuery()
            cn.Close()

            Return True

        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & sql, "Error in DBUtil.RunSQL", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

End Class
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ocsean
  • 35
  • 7
  • You cannot directly connect to a MDF file itself. You need to have a SQL Server running on your machine assuming you are connecting locally. – Ruben_PH Dec 22 '13 at 20:18
  • 2
    Using LocalDB allows you to connect directly to the .mdf file. – Ola Ekdahl Dec 22 '13 at 20:30
  • Double-check your connecting string. It looks like you have Integrated Security=True in there twice. – Ola Ekdahl Dec 22 '13 at 20:34
  • Thanks for the replies. I removed the Integrated Security=True that I had in twice and I am still receiving the same error. Thanks again for the kind help! – ocsean Dec 22 '13 at 20:44
  • 1
    "An error occurred creating the form" - suspect this indicates something has gone awry with the code in the .designer.vb file. – peterG Dec 22 '13 at 23:05
  • A stacktrace would be helpfull here – Koen Dec 24 '13 at 22:01

1 Answers1

0

Have you tied to set a breakpoint a cn.Open() and then step through the code to see where the error is occurring?

T Davey
  • 1,617
  • 1
  • 13
  • 14