Click here to Skip to main content
16,012,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem in compiling this code sqlException was unhandled Invalid object name dd_tb
Please can you help me to find the problem

VB
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
    Dim cn As SqlConnection
    Dim ds As DataSet
    Dim da As SqlDataAdapter
    Dim r As DataRow
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strconnection As String = "data source=localhost;initial catalog=master;integrated security=true"
        cn = New SqlConnection(strconnection)
        cn.Open()
        Try
            cn.ChangeDatabase("dd_db")
        Catch ex As Exception
            Try
                Dim command As New SqlClient.SqlCommand("create database dd_db", cn)
                command.ExecuteNonQuery()
                command.Connection.ChangeDatabase("dd_db")
                Dim command1 As New SqlClient.SqlCommand("create table dd_tb ([admi.num][int](10),[tran.num][int](4),[name][char](40),[age.reci][int](2),[sex.reci][char](8),[or.kid.d][char](22),[specify][char](50),[consang][char](20),[rec.b.g][char](8),[r_iggt][char](8),[r_iggr][char](12),[r_igmt][char](8),[r_igmr][char](12),[r_hcvt][char](8),[r_hcvr][char](12),[r_pcrt][char](8),[r_pcrr][char](12),[r_hbvt][char](8),[r_hbvr][char](12),[r_bpcrt][char](8),[r_bpcrr][char](12),[blood.gp][char](10),[rhla_aa][int](8),[rhla_ab][int](8),[rhla_ba][int](8),[rhla_bb][int](8),[rdr_a][int](8),[rdr_b][int](8),[prior.bl][char](8),[type.blo][char](15),[number.o][char](15),[num.bl.t][int](2),[datel.bl][date](10),[erythrop][char](8),[hypr_pre][char](8),[rec_schi][char](8),dia_rec][char](22),[sit_urn][char](12),[dial_pre][char](8))", cn)
                command1.ExecuteNonQuery()
            Catch ex1 As Exception
                MsgBox(ex.Message)
            End Try
        End Try
        da = New SqlDataAdapter("select * from dd_tb", cn)
        Dim x As New SqlCommandBuilder(da)
        ds = New DataSet()
        da.Fill(ds, "dd_tb")
    End Sub

[Edit]Code block added[/Edit]
Posted
Updated 21-Dec-12 23:09pm
v2
Comments
OriginalGriff 22-Dec-12 5:17am    
Where does it occur? Which line?

1 solution

I have a problem in compiling this code sqlException was unhandled Invalid object name dd_tb
1. It should not be compiling issue, it's a runtime issue... a logical flaw.
2. Issue is:
a. You define the connection object to work on database name 'master'.
b. You created a new table(dd_tb) but in different database (in 'dd_db').
c. You define an adapter that uses connection string that has 'master' as database. Thus, your select query throws an error as there is no such table in master.

You need to make sure that the Adapter query command runs on the same database where the table is created/present. Try putting this line just before Adapter definition:
C#
cn.ChangeDatabase("dd_db")
 
Share this answer
 
Comments
Member 8697827 22-Dec-12 6:02am    
Thank you very much for your reply
I tried tthis solution but still the same error
Sandeep Mewara 22-Dec-12 6:05am    
Go step wise and you will get it. To start with, make sure 'dd_db' exists and use the same in connection string - see what happens.

DEBUG and see where you fail. I already shared the loop-holes in your code.
Member 8697827 22-Dec-12 6:28am    
I search dd_db and it exist but I cannot find the dd_tb so I think the error is in the command of create table but i cannot find the error can you help me
Sandeep Mewara 22-Dec-12 6:29am    
Copy the query and run the raw query directly in your SQL DB. You will see if its incorrect and where.

:)
Member 8697827 22-Dec-12 6:32am    
I do it but it gives incorrect syntax near ']'.

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