Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
 
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 21-Dec-12 22:50pm
Edited 21-Dec-12 23:09pm
ProgramFOX124.6K
v2
Comments
OriginalGriff at 22-Dec-12 5:17am
   
Where does it occur? Which line?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
cn.ChangeDatabase("dd_db")
  Permalink  
Comments
Member 8697827 at 22-Dec-12 6:02am
   
Thank you very much for your reply
I tried tthis solution but still the same error
Sandeep Mewara at 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 at 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 at 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 at 22-Dec-12 6:32am
   
I do it but it gives incorrect syntax near ']'.
Sandeep Mewara at 22-Dec-12 6:51am
   
It always tells the location too. :doh:
 
You need to learn on how to read and find for issues based on trace provided. Ok, now, it looks like your CREATE table syntax is incorrect. SHould be like:
create table dd_tb (
[admi.num] int(10),
[tran.num] int(4), .,.......
 
Refer: http://www.w3schools.com/sql/sql_create_table.asp

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



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100