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:
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
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()
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()
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
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)
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()
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