Results 1 to 6 of 6

Thread: Create database programmatically

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2001
    Location
    Calgary
    Posts
    247

    Create database programmatically

    I got an error BC30506 and I do not understand this error because I am practicing I copy this code from a youtube demonstration.
    I am using VIsual studio 2019 and I wanna create an SQL database. I need help from an expert database developer I have 3 days trying to solve this and I cannot get it. Please help.


    My code is this.
    Code:
    Imports System.Data.SqlClient
    Public Class Form1
    
    
        Public Class Form2
            Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
                Dim cons1 As String = "Data source.;initial catalog=;integrated security=true"
                Dim conn1 As New SqlConnection(cons1)
                Dim cmd As SqlCommand
                Dim strsql1 As String
                conn1.Open()
                cmd = conn1.CreateCommand()
                strsql1 = "create database Appointments"
                cmd.CommandText = strsql1
                cmd.ExecuteNonQuery()
                conn1.Close()
                cons1 = "Data source.;initial catalog=Appointment;integrated security=true"
                conn1 = New SqlConnection(cons1)
                conn1.Open()
                cmd = conn1.CreateCommand()
                strsql1 = "Create table table1 (id int Not null primary key identity(1,1)," &
                "id2 int not null ," &
                "name1 Varchar(50) , " &
                "name2 Varchar(50) , " &
                "Tel1  Varchar(50) , " &
                "date1 date not null)"
    
                cmd.CommandText = strsql1
                cmd.ExecuteNonQuery()
                MessageBox.Show("Database Created Successfuly")
    
    
            End Sub
        End Class
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Last edited by Shaggy Hiker; Oct 25th, 2021 at 11:59 AM. Reason: Added CODE tags.
    mannyso

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,405

    Re: Create database programmatically

    Why do you have another "Private Sub Button1_Click ..." after the Form's class?
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,721

    Re: Create database programmatically

    According to the docs you're missing a "WithEvents" in your Declaration
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,526

    Re: Create database programmatically

    Quote Originally Posted by passel View Post
    Why do you have another "Private Sub Button1_Click ..." after the Form's class?
    Because this is why...

    Quote Originally Posted by Manny Somarriba View Post
    I copy this code from a youtube demonstration.


    Quote Originally Posted by Manny Somarriba View Post

    Imports System.Data.SqlClient
    Public Class Form1


    Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim cons1 As String = "Data source.;initial catalog=;integrated security=true"
    Dim conn1 As New SqlConnection(cons1)
    Dim cmd As SqlCommand
    Dim strsql1 As String
    conn1.Open()
    cmd = conn1.CreateCommand()
    strsql1 = "create database Appointments"
    cmd.CommandText = strsql1
    cmd.ExecuteNonQuery()
    conn1.Close()
    cons1 = "Data source.;initial catalog=Appointment;integrated security=true"
    conn1 = New SqlConnection(cons1)
    conn1.Open()
    cmd = conn1.CreateCommand()
    strsql1 = "Create table table1 (id int Not null primary key identity(1,1)," &
    "id2 int not null ," &
    "name1 Varchar(50) , " &
    "name2 Varchar(50) , " &
    "Tel1 Varchar(50) , " &
    "date1 date not null)"

    cmd.CommandText = strsql1
    cmd.ExecuteNonQuery()
    MessageBox.Show("Database Created Successfuly")


    End Sub
    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    You put one form into another...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,747

    Re: Create database programmatically

    Technically, you might be able to get away with that. The designer goes haywire, but I think I once created a private form inside a public form. Either that or it was a private form inside a non-form class, and I did that back in either VS2003 or VS2005, so things may have changed. It was just to try it out, too. There is no practical use for such a thing, and, as I said, the designer goes haywire.
    My usual boring signature: Nothing

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,405

    Re: Create database programmatically

    Bottom line, you didn't provide a link to the youtube demo.
    I would guess you copied it wrong, but can't verify that.

    Ok, I did a search and probably found the tutorial you were looking at.
    https://www.youtube.com/watch?v=EdsnsGvHn7Q

    The issue there is that in their example, they started with a Form2 as their primary form, they didn't have a Form1.
    In your case, you have a Form1, so you shouldn't have copied the Form2 Class declaration line and End Class into your code.
    You should have just inserted the code between "Class Form2" and "End Class" in the example in between your "Class Form1" and "End Class".

    Or you could have renamed your Form1 to Form2 in your project so that you had the same situation as the example.
    I tried your code as follows
    Code:
    Imports System.Data.SqlClient
    Public Class Form1
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim cons1 As String = "Data source=.;initial catalog=;integrated security=true"
        Dim conn1 As New SqlConnection(cons1)
        Dim cmd As SqlCommand
        Dim strsql1 As String
        conn1.Open()
        cmd = conn1.CreateCommand()
        strsql1 = "create database Appointments"
        cmd.CommandText = strsql1
        cmd.ExecuteNonQuery()
        conn1.Close()
        cons1 = "Data source.;initial catalog=Appointments;integrated security=true"
        conn1 = New SqlConnection(cons1)
        conn1.Open()
        cmd = conn1.CreateCommand()
        strsql1 = "Create table table1 (id int Not null primary key identity(1,1)," &
        "id2 int not null ," &
        "name1 Varchar(50) , " &
        "name2 Varchar(50) , " &
        "Tel1  Varchar(50) , " &
        "date1 date not null)"
    
        cmd.CommandText = strsql1
        cmd.ExecuteNonQuery()
        MessageBox.Show("Database Created Successfuly")
        conn1.Close()
    
      End Sub
    End Class
    I noticed a couple of typos in your code, i.e. you had "Data source." instead of "Data source=." in the first instance.
    You also named the database "Appointments" but then tried to open "Appointment".

    I guess the above fixes might work. When I tried to run it is said it couldn't connect to an SQL Server, which I assume is true because I don't work with databases and don't have an SQL server running.
    Last edited by passel; Oct 25th, 2021 at 10:50 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width