Locked Invalid object name 'dbo.tablename'

  • Sunday, September 11, 2011 6:29 PM
     
      Has Code

    Hi I'm new to VB, trying to connect vb to sql server using this code:

    Public Class _Default
        Inherits System.Web.UI.Page
    
        Dim insertQuery As String
        Dim updateQuery As String
        Dim deleteQuery As String
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    
            Dim DBC As SqlConnection = New SqlConnection("Data source=mypc\sqlexpress;Persist Security Info=True;Integrated Security=SSPI;Initial Catalog=master")
    
            insertQuery = "INSERT INTO [dbo].[contact] (name, number) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "' )"
    
            Dim myCommand As New SqlCommand(insertQuery)
            myCommand.Connection = DBC
            myCommand.Connection.Open()
            myCommand.ExecuteNonQuery()  '
            myCommand.Connection.Close()
    
        End Sub
    End Class
    

    However when I run it and enter some information to insert to the database, I got this error message:
    Server Error in '/' Application.
    Invalid object name 'dbo.contact'.
    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.
    

     

     

     

    This is how the database looks like:

     

    I searched for "invalid object name'dbo.tablename'" error but couldn't get it to work.
    Please advise.
    Thank you.



    • Edited by xbenchi Sunday, September 11, 2011 6:31 PM
    • Edited by xbenchi Sunday, September 11, 2011 6:31 PM
    • Edited by xbenchi Sunday, September 11, 2011 6:32 PM
    •  

All Replies

  • Sunday, September 11, 2011 8:43 PM
     
      Has Code

    Your query should look like this:

     insertQuery = "INSERT INTO [contact] (name, number) VALUES('" & TextBox1.Text & "','" & TextBox2.Text + "' )

    Also i can not find the database path in your SQL Connection String. There should be a database file path.

     

    Thanks.


    If my post answers your question then mark as answer.
  • Sunday, September 11, 2011 9:15 PM
     
      Has Code

    Your query should look like this:

     insertQuery = "INSERT INTO [contact] (name, number) VALUES('" & TextBox1.Text & "','" & TextBox2.Text + "' )

    Also i can not find the database path in your SQL Connection String. There should be a database file path.

     

    Thanks.


    If my post answers your question then mark as answer.


    Does this connection string look right?

    DBC.ConnectionString = "server=mypc\sqlexpress;Database=master;Persist Security Info=True;Integrated Security=SSPI"

    I tried it but still no luck. I must be missing something. Please adivse. Thank you.


    • Edited by xbenchi Sunday, September 11, 2011 9:31 PM
    •  
  • Sunday, September 11, 2011 10:56 PM
     
     Answered

    Hi,

    You are using Master database. Did you create any database? If so then it is in your App_Data folder in Solution Explorer. Please view in that folder and change the database name from Master.

     

    Thanks.


    If my post answers your question then mark as answer.
  • Monday, September 12, 2011 12:59 AM
     
     
     
    "xbenchi" wrote in message news:e8137142-c81d-451b-9136-9b7c00fff706...

    Your query should look like this:

     insertQuery = "INSERT INTO [contact] (name, number) VALUES('" & TextBox1.Text & "','" & TextBox2.Text + "' )

    Also i can not find the database path in your SQL Connection String. There should be a database file path.

     

    Thanks.


    If my post answers your question then mark as answer.


    Does this connection string look right?

    DBC.ConnectionString = "server=mypc\sqlexpress;Database=master;Persist Security Info=True;Integrated Security=SSPI"

    I tried it but still no luck. I must be missing something. Please adivse. Thank you.

    Don't think the database can be "master", as this is a system database

     



    Harry
  • Monday, September 12, 2011 11:53 PM
     
     

    Did you try include master.mdb?

     

    Thanks.


    If my post answers your question then mark as answer.
  • Saturday, April 14, 2012 1:43 PM
     
     
    You need to use parameters here. This code exposes you to a SQL injection attack. You should never use the value of user input directly in a query.