5 Replies - 344 Views - Last Post: 10 January 2019 - 01:39 AM Rate Topic: -----

#1 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 19-November 18

Error. Login Failed for User ''

Posted 09 January 2019 - 05:13 AM

Hello,

I created a windows form named FormDbConnect.vb where we use to login a database. It is work well.
Here is
Imports System
Imports System.IO
Imports System.Collections
Imports System.Configuration
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
...
...
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim oConn As SqlClient.SqlConnection
        Dim sConnString As String
        Dim sSQLInstance As String
        Dim sUserName As String
        Dim sPassword As String

        Try
            sConnString = "workstation id=" & txtWorkStation.Text & _
                            ";packet size=4096" & _
                            ";user id=" & txtUserName.Text & _
                            ";data source=" & txtSQLServerInstance.Text & _
                            ";persist security info=False" & _
                            ";initial catalog=" & txtDatabase.Text & _
                            ";Password=" & txtPassword.Text

            'oConn.DbServerType = BoDataServerTypes.

            oConn = New SqlClient.SqlConnection(sConnString)
            oConn.Open()
            If oConn.State = ConnectionState.Open Then
                'MessageBox.Show("Connected")
                Dim oStreamWriter As StreamWriter
                Dim sPath As String
                'sPath = IO.Directory.GetParent(Application.StartupPath).ToString & "\"
                sPath = Application.StartupPath & "\"
                Try
                    oStreamWriter = File.CreateText(sPath & "app")
                    encrypted = Encrypt(sConnString, G_PRIVATEKEY)
                    oStreamWriter.WriteLine(encrypted)

                    sSQLInstance = Encrypt(txtSQLServerInstance.Text, G_PRIVATEKEY)
                    oStreamWriter.WriteLine(sSQLInstance)

                    sUserName = Encrypt(txtUserName.Text, G_PRIVATEKEY)
                    oStreamWriter.WriteLine(sUserName)

                    sPassword = Encrypt(txtPassword.Text, G_PRIVATEKEY)
                    oStreamWriter.WriteLine(sPassword)

                    oStreamWriter.Flush()
                    oStreamWriter.Close()
                    oStreamWriter = Nothing

                    MessageBox.Show("Connected")
                    CType(Me.Owner, frmMain).ReLoad()
                    CType(Me.Owner, frmMain).Show()
                    Me.Close()
                Catch ex As Exception
                    MessageBox.Show("IO Failed" & Chr(13) & Chr(13) & ex.Message)
                End Try
            Else
                MessageBox.Show("Could not connect to Server")
            End If
        Catch ex As Exception
            MessageBox.Show("Could not connect to Server" & Chr(13) & Chr(13) & ex.Message)
        Finally
            oConn.Close()
            oConn = Nothing
        End Try
    End Sub




In the other form, I created a combobox and I use the code in this following link to fetch the data from the database into the combobox as combobox value:
https://www.dreaminc...nto-a-combobox/

I use this following code to connect the database so that my sql command works well :

Imports System.ComponentModel
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports System.IO

Public Class frmsppmgm
    Inherits System.Windows.Forms.Form
...
...

 Private Sub btnLoadGoodsIssue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadGoodsIssue.Click
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        If Me.txtGoodsIssueFile.Text.Trim = "" Then
            Me.Cursor = System.Windows.Forms.Cursors.Default
            MessageBox.Show("Please, choose a file first")
            Exit Sub
        End If
        'Combo box Code
        Dim sConnString As String
        sConnString = ";server=" & frmDBConnect.txtSQLServerInstance.Text & _
                            ";initial catalog=" & frmDBConnect.txtDatabase.Text & _
                            ";user id=" & frmDBConnect.txtUserName.Text & _
                            ";Password=" & frmDBConnect.txtPassword.Text
        Dim conn As New SqlConnection(sConnString)
        'create the SqlCommand object and set the sql query
        Dim cmd As New SqlCommand("SELECT [name] as 'Prc' FROM table Where Locked ='N' and Dimcode = '2'", conn) '<--or ("SELECT fname FROM users", conn)
        Dim names As New List(Of String)
        Try
            conn.Open()
            'create the SqlDataReader object to connect to our table
            Dim rd As SqlDataReader = cmd.ExecuteReader(Commandbehavior.CloseConnection)
            While rd.Read()
                names.Add(rd("Prc").ToString) '<-- retrieve just "fname" field
            End While
            rd.Close() '<--important
            conn.Close() '<--important
            Me.CmbTranUnit.Items.Clear()
            Me.CmbTranUnit.Items.AddRange(names.ToArray)
        Catch ex As System.Exception
            MessageBox.Show("Error" & vbLf & ex.Message & vbLf & "Trace: " & vbLf & ex.StackTrace)
        End Try
...        
...




please help me out
the code is given error Login Failed for User.
I have attached the print screen of error when I run the code. Thanks in advance


Rgds,
Steve

Attached image(s)

  • Attached Image

This post has been edited by Steve7419: 09 January 2019 - 05:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Error. Login Failed for User ''

#2 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,775
  • Joined: 12-December 12

Re: Error. Login Failed for User ''

Posted 09 January 2019 - 05:34 AM

It looks like the user(id) is blank. In the second form print out the value of frmDBConnect.txtUserName.Text.

It looks like you are referring directly to the (static) instance of the form named frmDBConnect, rather than the currently running instance. You need some reference to the currently running instance of the form in order to read values from it. I have a tutorial about communicating between forms in the VB.NET section.
Was This Post Helpful? 1
  • +
  • -

#3 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 19-November 18

Re: Error. Login Failed for User ''

Posted 09 January 2019 - 05:59 AM

Thanks a lot.

I have read the link you have provided. It means that I have to find the running instance but the running instance is stored in the app file and it is encrypted. Please CMIIW.

I do not want to hardcode the connection string because the application will be used in the other server and database.

Looking forward to your reply soon.

This post has been edited by andrewsw: 09 January 2019 - 06:31 AM
Reason for edit:: removed previous quote, use the Reply button

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,775
  • Joined: 12-December 12

Re: Error. Login Failed for User ''

Posted 09 January 2019 - 06:36 AM

Quote

but the running instance is stored in the app file and it is encrypted.

? Encrypted? I don't follow...

A running instance of a form is part of the Forms collection, and a reference to it can be provided in a second form's constructor, or the two forms can share a common data model.

Presuming, of course, that the two forms are still running within the same application.
Was This Post Helpful? 1
  • +
  • -

#5 Steve7419   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 19-November 18

Re: Error. Login Failed for User ''

Posted 09 January 2019 - 07:48 AM

View Postandrewsw, on 09 January 2019 - 06:36 AM, said:

Quote

but the running instance is stored in the app file and it is encrypted.

? Encrypted? I don't follow...

A running instance of a form is part of the Forms collection, and a reference to it can be provided in a second form's constructor, or the two forms can share a common data model.

Presuming, of course, that the two forms are still running within the same application.


Thanks again for your quick reply. I've got what your saying. I have worked on it. Herewith reply i post my solution:

Dim conn As New SqlConnection(("Server=" & Me.SBO_Active_Company.Server.ToString & _
                                       ";Database=" & Me.SBO_Active_Company.CompanyDB.ToString & _
                                       ";Integrated Security=True"))



I removed the sConnString from frmDbConnect.vb.
In all forms, there is code as follows:
Region " Private Attribute "
...
...
Private m_SBO_ActiveCompany As SAPbobsCOM.Company
...
#End Region



There is also code as follows:

#Region " Public Property "
...
...
Public Property SBO_Active_Company() As SAPbobsCOM.Company
        Get
            Return m_SBO_ActiveCompany
        End Get
        Set(ByVal Value As SAPbobsCOM.Company)
            m_SBO_ActiveCompany = Value
        End Set
    End Property
...
...
#End Region




I didn't notice the above codes before. The problem is now solved. Thanks a lot for your replies.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6746
  • View blog
  • Posts: 27,775
  • Joined: 12-December 12

Re: Error. Login Failed for User ''

Posted 10 January 2019 - 01:39 AM

Steve7419 Glad you sorted.

If you want to add something further to this thread then use the Reply button or the Fast Reply box, there is no need to always quote earlier content.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1