Invalid object name 'dbo.tablename'
-
Sunday, September 11, 2011 6:29 PM
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.
All Replies
-
Sunday, September 11, 2011 8:43 PM
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
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
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.- Proposed As Answer by Mike FengMicrosoft Contingent Staff, Moderator Tuesday, September 13, 2011 8:28 AM
- Marked As Answer by Mike FengMicrosoft Contingent Staff, Moderator Sunday, September 18, 2011 7:17 AM
-
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 PMYou 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.

