Hi all,
The code snippet is as shown below. I have an aspx page which is right below this line, where an object is created to establish connection to database.
---------------------------------------------------------------------------------------------------------------------------
Public Class TestDB
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim objDB As GetDBConn = Nothing
Dim strSQL As String = ""
objDB = New GetDBConn
strSQL = "SELECT TOP 1 Login FROM LoginTable"
Label1.Text = CStr(objDB.GetScalar(strSQL))
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
End Class
---------------------------------------------------------------------------------------------------------------------------
The code given below is the class through which database connection is established.
---------------------------------------------------------------------------------------------------------------------------
Public Class GetDBConn
Dim mConn As SqlConnection = Nothing
Dim mCmd As SqlCommand = Nothing
Dim mStrConn as String = ""
Sub New(Optional ByVal pFileName As String = "DBCredentials.FIL")
Try
Dim sr As New IO.StreamReader(HttpContext.Current.Server.MapPath("~") & "\App_Data\" & pFileName)
Dim i As Integer, buf As String, buf2 As String
Dim mServerName As String = ""
Dim mDBName As String = ""
Dim mUser As String = ""
Dim mPW As String = ""
Dim mStrConn As String = ""
While sr.EndOfStream = False
buf = sr.ReadLine
buf = DeEncrypt(buf)
i = InStr(buf, "=")
If i >= 1 Then
buf2 = UCase(Trim(Left(buf, i - 1)))
If buf2 = "SERVER" Then mServerName = Trim(Mid(buf, i + 1))
If buf2 = "DB" Then mDBName = Trim(Mid(buf, i + 1))
If buf2 = "LOGIN" Then mUser = Trim(Mid(buf, i + 1))
If buf2 = "PWD" Then mPW = Trim(Mid(buf, i + 1))
End If
End While
sr.Close()
mStrConn = "Data Source=" & mServerName & "; Initial Catalog=" & mDBName & "; User id=" & mUser & ";Password=" & mPW
If mConn Is Nothing Then
mConn = New System.Data.SqlClient.SqlConnection(mStrConn)
mConn.Open()
mCmd = New SqlCommand
mCmd.Connection = mConn
End If
Catch ex As Exception
Throw New System.Exception(ex.Message)
End Try
End Sub
Public Overloads Function GetScalar(ByVal strSQL As String) As Object
Try
mCmd.CommandText = strSQL
mCmd.Connection = mConn
Return mCmd.ExecuteScalar()
Catch ex As Exception
Throw New System.Exception(ex.Message)
Return Nothing
Finally
End Try
Return Nothing
End Function
End Class
---------------------------------------------------------------------------------------------------------------------------
Can you please help me with this.
The project works fine in my local system. But when I publish and host in on Azure, it gives an error saying
"Could not find a part of the path 'E:\sitesroot\0\App_Data\DBCredentials.FIL'. "
Regards
Sandeep