Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Run this application correctly from vb.net code
when build this application and compile Exe file
Run Exe file error display:
Cannot open database "COLORS". User login failed for user "Administrator"

please check copy past so that you's own system
so that clear problem

What I have tried:

VB.NET
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.IO

Public Class FrmColors

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim SQLConnectionString As String = ConfigurationManager.ConnectionStrings("RMS.My.MySettings.StupConnection").ConnectionString
        System.Configuration.ConfigurationManager.AppSettings.Get("SQLSqriptRead")

        CmdRefresh.Enabled = False
        PictureImage.SizeMode = PictureBoxSizeMode.CenterImage
        PictureImage.Image = Global.Colors.My.Resources.Colorful
        Me.Icon = Global.Colors.My.Resources.Color_line_Icon

        Using SqlCon As SqlConnection = New SqlConnection(SQLConnectionString)
            Dim StrDatabase As String

            Dim CreateDirectory As DirectoryInfo = Directory.CreateDirectory(Application.StartupPath & "\Colors")

            Dim ColorsDataBaseMDF As String = "Colors.mdf"
            Dim ColorsDataBaseLOG As String = "Colors.log"

            StrDatabase = "CREATE DATABASE COLORS ON PRIMARY (NAME = SQLSqript_Data, FILENAME = '" & Application.StartupPath & "\Colors\" & ColorsDataBaseMDF & "', " & _
           " SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) LOG ON(NAME = SQLSqript_Log, FILENAME = '" & Application.StartupPath & "\Colors\" & ColorsDataBaseLOG & "'," & _
           " SIZE = 1MB,MAXSIZE = 5MB, FILEGROWTH = 10%) "

            Dim cmd As SqlCommand = New SqlCommand(StrDatabase, SqlCon)
            Try
                cmd.Connection.Open()
                cmd.ExecuteNonQuery()
                cmd.Connection.Close()
                SqlCon.Close()
            Catch
                MsgBox("Record already exist, delete and refresh to continue :", MsgBoxStyle.Critical)
            End Try
        End Using

        Dim SQLConnectionStringTeble As String = "Data Source=SYSTEM-PC\SQLEXPRESS;Initial Catalog=COLORS;User ID=Administrator;Password=Admin"

        Using SqlConTable As SqlConnection = New SqlConnection(SQLConnectionStringTeble)

            Try
                Dim StrTable As String

                '**************************** New table "Color" create *************************************

                StrTable = "CREATE TABLE [dbo].[ColorRange]([SRNO] [nvarchar](50) NULL,[Color] [nvarchar](50) NULL,[Palettes] [nvarchar](50) NULL) ON [PRIMARY]"
                Dim cmdTable As SqlCommand = New SqlCommand(StrTable, SqlConTable)

                cmdTable.Connection.Open()
                cmdTable.ExecuteNonQuery()

                '**************************** Insert record in script table *************************************
                Dim First_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'1', N'White', N'Maroon')", SqlConTable)
                First_Query.ExecuteNonQuery()

                Dim Second_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'2', N'Red', N'Purple')", SqlConTable)
                Second_Query.ExecuteNonQuery()

                '**************************** Display record in DataGridView1 *******************************************

                Dim ds_Colors = New DataSet
                Dim tables = ds_Colors.Tables

                Dim da_Colors = New SqlClient.SqlDataAdapter("Select SRNO,Color,Palettes from COLORS.dbo.ColorRange order by SRNO", SqlConTable)
                da_Colors.Fill(ds_Colors, "ColorRange")

                Dim ViewColors As New DataView(tables(0))
                DataGridView1.DataSource = ViewColors

                DataGridView1.AllowUserToAddRows = False
                DataGridView1.ReadOnly = True
                DataGridView1.Rows(0).Cells(0).Selected = False
                SqlConTable.Close()

            Catch
                'MsgBox(" Already installed database", MsgBoxStyle.Critical)
            End Try
        End Using

    End Sub

    Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
        DataGridView1.Item(e.ColumnIndex, e.RowIndex).ToolTipText = "These rows has been freeze"
    End Sub

    Private Sub CmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdRefresh.Click
        CmdDelete.Enabled = True
        CmdRefresh.Enabled = False

        Dim SQLConnectionStringTable As String = "Data Source=SYSTEM-PC\SQLEXPRESS;Initial Catalog=COLORS;User ID=Administrator;Password=Admin"

        Using SqlConTable As SqlConnection = New SqlConnection(SQLConnectionStringTable)

            '**************************** Insert record in script table *************************************
            SqlConTable.Open()

            Dim First_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'1', N'White', N'Maroon')", SqlConTable)
            First_Query.ExecuteNonQuery()

            Dim Second_Query As New SqlCommand("INSERT [dbo].[ColorRange] ([SRNO], [Color], [Palettes]) VALUES (N'2', N'Red', N'Purple')", SqlConTable)
            Second_Query.ExecuteNonQuery()
            '**************************** Insert process complete *******************************************

            Dim ds_Colors = New DataSet
            Dim tables = ds_Colors.Tables

            Dim da_Colors = New SqlClient.SqlDataAdapter("Select SRNO,Color,Palettes from COLORS.dbo.ColorRange order by SRNO", SqlConTable)
            da_Colors.Fill(ds_Colors, "ColorRange")

            Dim ViewColors As New DataView(tables(0))
            DataGridView1.DataSource = ViewColors

            DataGridView1.AllowUserToAddRows = False
            DataGridView1.ReadOnly = True
            DataGridView1.Rows(0).Cells(0).Selected = False
            SqlConTable.Close()

        End Using
    End Sub

    Private Sub CmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDelete.Click
        CmdRefresh.Enabled = True
        CmdDelete.Enabled = False
        DataGridView1.DataSource = Nothing

        Dim SQLConnectionStringTable As String = "Data Source=SYSTEM-PC\SQLEXPRESS;Initial Catalog=COLORS;User ID=Administrator;Password=Admin"

        Using SqlConTable As SqlConnection = New SqlConnection(SQLConnectionStringTable)
            SqlConTable.Open()
            Dim DeleteColorRange As New SqlCommand("Delete from COLORS.dbo.ColorRange", SqlConTable)
            DeleteColorRange.ExecuteNonQuery()
        End Using

    End Sub

    Private Sub CmdDelete_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdDelete.MouseHover
        CmdDelete.ForeColor = Color.Fuchsia
    End Sub

    Private Sub CmdDelete_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdDelete.MouseLeave
        CmdDelete.ForeColor = Color.Black
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
    End Sub

End Class
Posted
Updated 22-Sep-18 23:44pm
v2
Comments
Richard Deeming 24-Sep-18 14:11pm    
REPOST
You have already posted this, and been given the answer:
https://www.codeproject.com/Questions/1261063/Run-time-from-VB-NET-no-error[^]

1 solution

Look at the error message:
User login failed for user "Administrator"
It's pretty self explanatory.
You need to talk to the DB admin and find out what the username and password combination you need for your connection string actually is - it's very unlikely to be "User ID=Administrator;Password=Admin"
 
Share this answer
 
Comments
Computechsoft 23-Sep-18 7:19am    
How to talk to the DB admin
you can tell any link so that talk to him
OriginalGriff 23-Sep-18 7:33am    
Um. You do realise that there is a somebody who "looks after" each DB installation, or there should be? No, I can't give you a link to the guy in charge of your particular SQL Server installation ...

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