Answered by:
Can't connect on my textbox

Question
-
Hi everyone,
I have a form named FrmRemoteDesktopClient, on this form, I have several fields that are linked to a table named RemoveDesktop (This is an SQL Server table)
When I select a row in the datagridview, the fields are updated based on the above table, when I click on the connect button, everything works if I put the full path in the code on my BtnConnect button, for example if I put:
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click Process.Start("Cmd.exe", "/C mstsc.exe C:\ACRegister\RemoteServers\ServeurPCKARO.rdp") End Sub
The RDC works end I can connect without a problem
I arrange the code so the field ServerFullPathTB.Text equal to
C:\ACRegister\RemoteServers\ServeurPCKARO.rdp"
and I modify my code like this:
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click Process.Start("Cmd.exe", "/C mstsc.exe & ServerFullPathTB.Text") End Sub
As you can see in the form below, the ServerFullPathTB.Text is exactly the path, but when I click on the BtnConnect, the RDC opens, but the file itself isn't process meaning that it will show the last connection instead of using the fields in the rdp file.
Wednesday, September 9, 2020 3:36 PM
Answers
-
Hi,
Hundreds of views and no replies, may be it is to complicated, here is the solution I found after many hours of trials and errors.
athe code:
Imports System.Net.Sockets Imports System.Threading Imports System.Drawing Imports System.Runtime.Serialization.Formatters.Binary Public Class FrmRemoteDesktopClient Private SQL As New SQLControl Public Sub LoadGrid(Optional Query As String = "") If Query = "" Then SQL.ExecQuery("SELECT * FROM RemoteDesktop;") Else SQL.ExecQuery(Query) End If 'ERROR HANDLING If SQL.HasException(True) Then Exit Sub With DataGridView1 .AutoGenerateColumns = True .DataSource = SQL.DBDT .Columns(0).HeaderText = "ID" .Columns(1).HeaderText = "ClientName" .Columns(2).HeaderText = "ServerName" .Columns(3).HeaderText = "UserName" .Columns(4).HeaderText = "IPAddress" .Columns(5).HeaderText = "Port" .Columns(0).Width = 75 .Columns(1).Width = 200 .Columns(2).Width = 200 .Columns(3).Width = 200 .Columns(4).Width = 200 .Columns(5).Width = 100 End With End Sub Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click Dim process = New System.Diagnostics.Process() process.StartInfo = New ProcessStartInfo With { .FileName = "mstsc", .Arguments = "/v:" & IPAddressTB.Text } process.Start() End Sub Public Sub LoadFirstRecord() Try 'CLEAR EXISTING RECORD If SQL.DBDS IsNot Nothing Then SQL.DBDS.Clear() End If SQL.RunQuery("SELECT * FROM RemoteDesktop Where ID = 1") If SQL.DBDS.Tables(0).Rows(0).Item(0) >= 1 Then Me.IDTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("ID") Me.ClientNameTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("ClientName") Me.ServerNameTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("ServerName") Me.UsernameTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("Username") Me.IPAddressTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("IPAddress") Me.PortTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("Port") Else MsgBox("There Is no connection 1, please ask the system administrator!", MsgBoxStyle.Critical, "NO RECORD FOUND") End If If SQL.HasException(True) Then Exit Sub Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub FrmRemoteDesktopClient_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'TODO: cette ligne de code charge les données dans la table 'AcDataDS.RemoteDesktop'. Vous pouvez la déplacer ou la supprimer selon les besoins. Me.RemoteDesktopTableAdapter.Fill(Me.AcDataDS.RemoteDesktop) Me.CountRecords.Text = BindingNavigatorCountItem.ToString LoadGrid() Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString LoadClientsNames() LoadFirstRecord() ButtonFirst.PerformClick() End Sub Private Sub FindItem() SQL.AddParam("@item", "%" & TxtFilterTB.Text & "%") LoadGrid("SELECT * FROM RemoteDesktop WHERE ClientName LIKE @item;") End Sub Private Sub IDTB_TextChanged(sender As Object, e As EventArgs) Handles IDTB.TextChanged Me.CheckBox1.Checked = False End Sub Public Sub LoadClientsNames() Try 'REFRESH COMBOBOX ClientNameTB.Items.Clear() 'RUN QUERY SQL.ExecQuery("Select DISTINCT ID, ClientName FROM RemoteDesktop ORDER BY ID ASC;") If SQL.HasException(True) Then Exit Sub 'LOOP ROW & ADD TO COMBOBOX For Each r As DataRow In SQL.DBDT.Rows ClientNameTB.Items.Add(r("ClientName").ToString) Next If SQL.HasException(True) Then Exit Sub Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click Me.Close() End Sub Private Sub BtnNormal_Click(sender As Object, e As EventArgs) Handles BtnNormal.Click WindowState = FormWindowState.Normal Me.BtnMaximize.Visible = True Me.BtnNormal.Visible = False End Sub Private Sub BtnMinimize_Click(sender As Object, e As EventArgs) Handles BtnMinimize.Click WindowState = FormWindowState.Minimized End Sub Private Sub BtnMaximize_Click(sender As Object, e As EventArgs) Handles BtnMaximize.Click WindowState = FormWindowState.Maximized Me.BtnNormal.Visible = True Me.BtnMaximize.Visible = False End Sub Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click Me.IDTB.BackColor = Color.Red Me.IDTB.ForeColor = Color.Red If String.IsNullOrEmpty(IDTB.Text.Trim()) Or String.IsNullOrEmpty(ClientNameTB.Text.Trim()) Or String.IsNullOrEmpty(ServerNameTB.Text.Trim()) Or String.IsNullOrEmpty(UsernameTB.Text.Trim()) Or String.IsNullOrEmpty(IPAddressTB.Text.Trim()) Or String.IsNullOrEmpty(PortTB.Text.Trim()) Then MessageBox.Show("Please all fields must be completed To add a remote connection.", "ACRegister message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If Try Me.AddModeCB.Checked = True IDTB.Text = "" ClientNameTB.Text = "" ServerNameTB.Text = "" UsernameTB.Text = "" IPAddressTB.Text = "" PortTB.Text = "" BtnAdd.Text = "Add record" BtnSave.Text = "Save record ()" BtnDelete.Text = "Delete record ()" Me.ClientNameTB.Select() Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString Catch ex As Exception End Try End Sub Private Sub ShowFrmMsgSmallerSaved() Try Dim SmallerMsg As New FrmMsgSmaller FrmMsgSmaller.Close() FrmMsgSmaller.TextBox1.Text = "" FrmMsgSmaller.TextBox1.Text = "YOUR RECORDS WERE SAVED!" FrmMsgSmaller.Show() Dim SW2 As New Stopwatch SW2.Start() Do Application.DoEvents() Loop Until SW2.ElapsedMilliseconds >= 1000 FrmMsgSmaller.Hide() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub ShowFrmMsgSmallerUpdated() Try Dim SmallerMsg As New FrmMsgSmaller FrmMsgSmaller.Close() FrmMsgSmaller.TextBox1.Text = "" FrmMsgSmaller.TextBox1.Text = "YOUR RECORDS WERE UPDATED!" FrmMsgSmaller.Show() Dim SW2 As New Stopwatch SW2.Start() Do Application.DoEvents() Loop Until SW2.ElapsedMilliseconds >= 600 FrmMsgSmaller.Hide() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub ShowFrmMsgSmallerAdded() Try Dim SmallerMsg As New FrmMsgSmaller FrmMsgSmaller.Close() FrmMsgSmaller.TextBox1.Text = "" FrmMsgSmaller.TextBox1.Text = "YOUR New RECORD WAS ADDED To CATEGORIES!" FrmMsgSmaller.Show() Dim SW2 As New Stopwatch SW2.Start() Do Application.DoEvents() Loop Until SW2.ElapsedMilliseconds >= 600 FrmMsgSmaller.Hide() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub ShowFrmMsgSmallerDeleted() Try Dim SmallerMsg As New FrmMsgSmaller FrmMsgSmaller.Close() FrmMsgSmaller.TextBox1.Text = "" FrmMsgSmaller.TextBox1.Text = "THE SELECTED RECORD WAS DELETED!" FrmMsgSmaller.Show() Dim SW2 As New Stopwatch SW2.Start() Do Application.DoEvents() Loop Until SW2.ElapsedMilliseconds >= 600 FrmMsgSmaller.Hide() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub ResetMe() IDTB.Text = "" ClientNameTB.Text = "" ServerNameTB.Text = "" UsernameTB.Text = "" IPAddressTB.Text = "" PortTB.Text = "" BtnAdd.Text = "Add record" BtnSave.Text = "Save record ()" BtnDelete.Text = "Delete record ()" Me.CheckBox1.Checked = False End Sub Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click If DataGridView1.Rows.Count = 0 Then MsgBox(DataGridView1.Rows.Count.ToString() & " No rows found.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Auto Cash Register information") Exit Sub End If If BtnDelete.Text.Equals("Delete record ()") Then MessageBox.Show("Please Select an item from the list.", "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If Try If MsgBox("The selected server will be delete! ARE YOU SURE YOU WANT To Do THAT, THIS CAN'T BE UNDONE!", MsgBoxStyle.YesNo, "DELETE THIS SERVER?") = vbYes Then SQL.AddParam("@ID", IDTB.Text) SQL.ExecQuery("DELETE FROM RemoteDesktop WHERE ID = @ID;") Else MessageBox.Show("The deletion was aborted!.", "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) LoadGrid() Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString ResetMe() BtnRefresh.PerformClick() Exit Sub End If ShowFrmMsgSmallerDeleted() LoadGrid() Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString ResetMe() BtnRefresh.PerformClick() If SQL.HasException(True) Then Exit Sub Catch ex As Exception End Try End Sub Private Sub UpdateServers() 'ADD SQL PARAMS & RUN THE COMMAND Try SQL.AddParam("@ID", IDTB.Text) SQL.AddParam("@ClientName", ClientNameTB.Text) SQL.AddParam("@ServerName", ServerNameTB.Text) SQL.AddParam("@Username", UsernameTB.Text) SQL.AddParam("@IPAddress", IPAddressTB.Text) SQL.AddParam("@Port", PortTB.Text) SQL.ExecQuery("UPDATE RemoteDesktop " & "SET ClientName=@ClientName,ServerName=@ServerName,Username=@Username,IPAddress=@IPAddress, " & "Port=@Port WHERE ID=@ID;") If SQL.HasException(True) Then Exit Sub Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click If DataGridView1.Rows.Count = 0 Then MsgBox(DataGridView1.Rows.Count.ToString() & " No rows found.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Auto Cash Register information") Exit Sub End If If String.IsNullOrEmpty(ClientNameTB.Text.Trim()) Or String.IsNullOrEmpty(ServerNameTB.Text.Trim()) Or String.IsNullOrEmpty(UsernameTB.Text.Trim()) Or String.IsNullOrEmpty(IPAddressTB.Text.Trim()) Or String.IsNullOrEmpty(PortTB.Text.Trim()) Then MessageBox.Show("Please all fields must be completed to use the Save button.", "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If Try If AddModeCB.Checked = False Then UpdateServers() ShowFrmMsgSmallerSaved() LoadGrid() Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString Me.CheckBox1.Checked = True Me.BtnRefresh.PerformClick() Me.CheckBox1.Checked = True End If If AddModeCB.Checked = True Then SQL.AddParam("@ID", IDTB.Text) SQL.AddParam("@ClientName", ClientNameTB.Text) SQL.AddParam("@ServerName", ServerNameTB.Text) SQL.AddParam("@Username", UsernameTB.Text) SQL.AddParam("@IPAddress", IPAddressTB.Text) SQL.AddParam("@Port", PortTB.Text) SQL.ExecQuery("INSERT INTO RemoteDesktop(ClientName,ServerName,Username,IPAddress,Port) " & "VALUES (@ClientName,@ServerName,@Username,@IPAddress,@Port) ", True) ShowFrmMsgSmallerSaved() Me.IDTB.BackColor = Color.White Me.IDTB.ForeColor = Color.Black LoadGrid() Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString Me.CheckBox1.Checked = True Me.BtnRefresh.PerformClick() Me.CheckBox1.Checked = True End If Catch ex As Exception End Try End Sub Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click Me.Controls.Clear() InitializeComponent() FrmRemoteDesktopClient_Load(e, e) End Sub Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick Dim dgv As DataGridView = DataGridView1 Try If e.RowIndex <> -1 Then Me.IDTB.Text = Convert.ToString(dgv.CurrentRow.Cells(0).Value) BtnSave.Text = "Save record (" & Me.IDTB.Text & ")" BtnDelete.Text = "Delete record (" & Me.IDTB.Text & ")" ClientNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(1).Value) ServerNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(2).Value) UsernameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(3).Value) IPAddressTB.Text = Convert.ToString(dgv.CurrentRow.Cells(4).Value) PortTB.Text = Convert.ToString(dgv.CurrentRow.Cells(5).Value) Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString Me.CheckBox1.Checked = False End If If SQL.HasException(True) Then Exit Sub Catch ex As Exception End Try End Sub Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged Dim dgv As DataGridView = DataGridView1 Dim MyRowIndex As String MyRowIndex = Me.IDTB.Text Try If MyRowIndex <> -1 Then Me.IDTB.Text = Convert.ToString(dgv.CurrentRow.Cells(0).Value) BtnSave.Text = "Save record (" & Me.IDTB.Text & ")" BtnDelete.Text = "Delete record (" & Me.IDTB.Text & ")" ClientNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(1).Value) ServerNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(2).Value) UsernameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(3).Value) IPAddressTB.Text = Convert.ToString(dgv.CurrentRow.Cells(4).Value) PortTB.Text = Convert.ToString(dgv.CurrentRow.Cells(5).Value) Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString Me.CheckBox1.Checked = False Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString Me.CheckBox1.Checked = False End If If SQL.HasException(True) Then Exit Sub Catch ex As Exception End Try End Sub Private Sub ButtonFirst_Click(sender As Object, e As EventArgs) Handles ButtonFirst.Click BindingNavigatorMoveFirstItem.PerformClick() Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource End Sub Private Sub ButtonPrevious_Click(sender As Object, e As EventArgs) Handles ButtonPrevious.Click If Me.IDTB.Text = 1 Then MsgBox("You are at the first record of the dataset!", MsgBoxStyle.Information, "FIRST RECORD") Exit Sub End If BindingNavigatorMovePreviousItem.PerformClick() Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource End Sub Private Sub ButtonNext_Click(sender As Object, e As EventArgs) Handles ButtonNext.Click BindingNavigatorMoveNextItem.PerformClick() Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource End Sub Private Sub ButtonLast_Click(sender As Object, e As EventArgs) Handles ButtonLast.Click BindingNavigatorMoveLastItem.PerformClick() Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource End Sub Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click If DataGridView1.Rows.Count = 0 Then MsgBox(DataGridView1.Rows.Count.ToString() & " No rows found.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Auto Cash Register information") Exit Sub End If If String.IsNullOrEmpty(TxtFilterTB.Text.Trim()) Then MessageBox.Show("You can't search from an empty filter box!.", "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Exit Sub End If Try FindItem() If SQL.HasException(True) Then Exit Sub Catch ex As Exception MsgBox("An error occured: " & ex.Message.ToString(), MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical, "Search Error") End Try End Sub End Class
- Marked as answer by Claude du Québec Friday, September 11, 2020 1:37 PM
Friday, September 11, 2020 1:37 PM