Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all.

I'm trying to build a class to my program that will work with a data base unfortunetly I ran into Runtime error named:

"SqlException was unhandled - Invalid object name 'ProgDB'."

*ProgDB is the name of my tabale at the Database

The class objectives are:

1) Building "Insert" function.
2) Building "Exist" function.
3) Building "Get" function.
4) Understanding The Code.

heres my code so far:

VB
Public Class DBClass
    Dim sqlConn As New SqlConnection
    Dim sqlCMD As New SqlCommand
    Dim sqlDataAdapter As New SqlDataAdapter
    Dim dbPath As String = My.Settings.DBPath.ToString()
    Dim connectString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & dbPath & ";Integrated Security=True;Connect Timeout=30;User Instance=True"
    Dim ds As DataSet
    Function IDExist(ByVal iDNum As String) As Boolean
        sqlConn.ConnectionString = connectString
        sqlConn.Open()
        Dim strSQL As String = "SELECT IDNum FROM ProgDB WHERE (IDNum = " & iDNum & ")"
        sqlCMD.Connection = sqlConn
        sqlCMD.CommandText = strSQL
        Dim i As String = sqlCMD.ExecuteScalar
        Dim exist = True
        If i <> Nothing Then
            exist = False
        End If
        sqlConn.Close()
        Return exist
    End Function


Thank you in advance Tsahi
Posted
Updated 19-Sep-11 12:08pm
v3

Hi
Error clearly says it couldn't find table from the details you provided,
Are you sure you connected to same DB as table ProgDB exists?
 
Share this answer
 
Comments
tsahi-al 20-Sep-11 4:50am    
progDB Exist, Ive used the Database Query's Allthough I know SQL pretty good I'm not writing them,

Do you need any value of Syntax?

Tsahi.
I know what the problem is, for some reason My settings dose'nt save the Path to the DB and therefor my connection string has no path Value, when i set the Path manualy it works like a charm!

now i need to figure out Why it dose'nt save the path at my settings

it saves the ImgOutput Path but not the DB Path

heres the code:

VB
Private Sub Settings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DBBox.Text = My.Settings.DBPath.ToString()
        ImgBox.Text = My.Settings.ImgOutput.ToString()
    End Sub

    Private Sub DoneBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DoneBTN.Click
        My.Settings.DBPath = DBBox.Text
        My.Settings.ImgOutput = ImgBox.Text
        MsgBox("Settings saved successfully!")
        Me.Close()
    End Sub
 
Share this answer
 
Try this.
VB
Public Class DBClass
    Dim sqlConn As New SqlConnection
    Dim sqlCMD As New SqlCommand
    Dim sqlDataAdapter As New SqlDataAdapter
    Dim dbPath As String = My.Settings.DBPath.ToString()
    Dim connectString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & dbPath & ";Integrated Security=True;Connect Timeout=30;User Instance=True"
    Dim ds As DataSet
    Function IDExist(ByVal iDNum As String) As Boolean
        sqlConn.ConnectionString = connectString
        sqlConn.Open()
        Dim strSQL As String = "SELECT IDNum FROM ProgDB WHERE (IDNum = @IDNum)"
        sqlCMD.Connection = sqlConn
        sqlCMD.CommandText = strSQL
        sqlCMD.CommandTypte = CommandType.Text
        sqlCMD.Parameters.AddWithValue("@IDNum",iDNum)
        Dim i As String = sqlCMD.ExecuteScalar
        Dim exist = True
        If i <> Nothing Then
            exist = False
        End If
        sqlConn.Close()
        Return exist
    End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900