الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

أريد حل لهذا ال Error وما هو سببه

مغلق
بدأه لالا في 30 أبريل 2006 · 14 رد · 2,287 مشاهدة · في ASP.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته..

أريد معرفة حل هذا Error وما هو سببه..

Server Error in '/login' Application.

________________________________________

ExecuteReader: Connection property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.

Source Error:

Line 22: conn.Connection = Connection

Line 23: conn.CommandText = sql

Line 24: Return conn.ExecuteReader

Line 25: End Function

Line 26: Public Shared Function dispose()

Source File: c:\inetpub\wwwroot\login\DAL.vb Line: 24

Stack Trace:

[invalidOperationException: ExecuteReader: Connection property has not been initialized.]

System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing)

System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)

System.Data.SqlClient.SqlCommand.ExecuteReader()

login.DAL.excuteSql(String sql) in c:\inetpub\wwwroot\login\DAL.vb:24

login.SignOn.login(String username, String password) in c:\inetpub\wwwroot\login\SignOn.aspx.vb:41

login.SignOn.butSignOn_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\login\SignOn.aspx.vb:123

System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

System.Web.UI.Page.ProcessRequestMain()

علما بأن الكود الموجود في DAL.vb هو:

Public Class DAL

Private Shared Connection As System.Data.SqlClient.SqlConnection

Public Shared Function initialize() As Boolean

If Connection Is Nothing Then

Dim Connection As New SqlClient.SqlConnection

Dim strConnectionString As String = "Server=SALMA-H1STH3TGR;DataBase=CASFSU;Trusted_Connection=True"

Connection.ConnectionString = strConnectionString

Connection.Open()

ElseIf Connection.State <> ConnectionState.Open Then

Connection.ConnectionString = "Server=SALMA-H1STH3TGR;DataBase=CASFSU;Trusted_Connection=True"

Connection.Open()

End If

Return True

End Function

Public Shared Function excuteSql(ByVal sql As String) As SqlClient.SqlDataReader

initialize()

Dim conn As New SqlClient.SqlCommand

conn.CommandType = CommandType.Text

conn.Connection = Connection

conn.CommandText = sql

Return conn.ExecuteReader

End Function

Public Shared Function dispose()

Connection.Close()

Connection.Dispose()

End Function

End Class

#3

يتم استدعاؤه في Function الـ excuteSql .. وFunction الـ excuteSql يتم استدعاؤها في Function الـ login

Private Function login(ByVal username As String, ByVal password As String) As Boolean

Dim dataReader As SqlDataReader

Dim sql As String = "SELECT Count(*) FROM Users WHERE ((UserName= '" & txtName.Text & "') AND (Password='" & txtPassword.Text & "'))"

dataReader = DAL.excuteSql(sql)

If (dataReader.HasRows) Then

dataReader.Read()

If (CInt(dataReader(0)) > 0) Then

'System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, False)

Response.Redirect("Register.aspx")

dataReader.Close()

Return True

End If

dataReader.Close()

Connection.Close()

Else

Return False

End If

End Function

Private Function signOut() As Boolean

System.Web.Security.FormsAuthentication.SignOut()

'Response.Redirect("UseLogin.aspx")

Return True

End Function

ويتم استدعاء Function الـ login في زر الـ login

Private Sub butlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSignOn.Click

If (login(txtName.Text, txtPassword.Text)) Then

lblLogin.Text = "Welcome " & Me.User.Identity.Name

Else

lblLogin.Text = "Please Enter valid credintials"

End If

End Sub

#5

لقد أرفقت الكود كاملا.. أشكرك على اهتمامك..

Login.txt

#6

السطر التالي في التابع initialize هو السبب:

 If Connection Is Nothing Then
            // السطر التالي هو سبب الخطأ
            Dim Connection As New SqlClient.SqlConnection

والسبب هو أنك أعدتي تعريف المتحول Connection ضمن هذا التابع أي أن المتحول Connection داخل التابع initialize هو غير المتحول المعرف خارج التابع initialize والحل هو حذف Dim من السطر السابق وكتابته كما يلي:

 If Connection Is Nothing Then
            Connection  = New SqlClient.SqlConnection
#7

ممممممم الكود هيك بيتعب العيون بدون تنسيق .. عموما انا ما لاحظت وجود Connection.Open قبل استدعاء ال ExecuteReader

يعني هيك رح يشتغل :

Private Function login(ByVal username As String, ByVal password As String) As Boolean

Dim dataReader As SqlDataReader 

Dim sql As String = "SELECT Count(*) FROM Users WHERE ((UserName= '" & txtName.Text & "') AND (Password='" & txtPassword.Text & "'))"

Connection.Open()


dataReader = DAL.excuteSql(sql)
If (dataReader.HasRows) Then
dataReader.Read()
If (CInt(dataReader(0)) > 0) Then
'System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, False)
Response.Redirect("Register.aspx")
dataReader.Close()
Return True
End If
dataReader.Close()
Connection.Close()
Else
Return False
End If
End Function

Private Function signOut() As Boolean
System.Web.Security.FormsAuthentication.SignOut()
'Response.Redirect("UseLogin.aspx")

Return True
End Function

You can fly while you can touch the sky

#8

ألحين طلع هذا الـ Error

Server Error in '/login' Application.

--------------------------------------------------------------------------------

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested Url: /login/default.aspx

--------------------------------------------------------------------------------

Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

مع أنه لايوجد عندي صفحة بهذا الإسم..

وأرفقت لكم الـ Web.config

لربما يكون الخطأ فيه

Web.config.txt

#10

أخوي محمد إذا أضفت Connection.Open()

يطلع هذا Error

Server Error in '/login' Application.

--------------------------------------------------------------------------------

The ConnectionString property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error:

Line 45: 'conn.Connection = Connection

Line 46: 'conn.CommandText = sql

Line 47: Connection.Open()

Line 48:

Line 49: dataReader = DAL.excuteSql(sql)

Source File: c:\inetpub\wwwroot\login\SignOn.aspx.vb Line: 47

Stack Trace:

[invalidOperationException: The ConnectionString property has not been initialized.]

System.Data.SqlClient.SqlConnection.Open()

login.SignOn.login(String username, String password) in c:\inetpub\wwwroot\login\SignOn.aspx.vb:47

login.SignOn.butSignOn_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\login\SignOn.aspx.vb:135

System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

System.Web.UI.Page.ProcessRequestMain()

#11

قبل ما أضيف Connection.Open()

كان يشتغل إذا وضعت في هذا الكود

Private Function login(ByVal username As String, ByVal password As String) As Boolean

Dim dataReader As SqlDataReader

Dim sql As String = "SELECT Count(*) FROM Users WHERE ((UserName= '" & txtName.Text & "') AND (Password='" & txtPassword.Text & "'))"

Connection.Open()

dataReader = DAL.excuteSql(sql)

If (dataReader.HasRows) Then

dataReader.Read()

If (CInt(dataReader(0)) > 0) Then

System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, False)

dataReader.Close()

Return True

End If

dataReader.Close()

Connection.Close()

Else

Return False

End If

End Function

Private Function signOut() As Boolean

System.Web.Security.FormsAuthentication.SignOut()

'Response.Redirect("UseLogin.aspx")

Return True

End Function

*************************************************8

السطر Response.Redirect("Register.aspx")

أما إذا وضعت السطر هذا

System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, False)

يطلع هذا الـ Error

Server Error in '/login' Application.

--------------------------------------------------------------------------------

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested Url: /login/default.aspx

ما المشكلة؟؟

#13

بالعكس أخي مشكوووووووووووور جدا قرأتها وحلت لي المشكلة الأولى وهو الـ Error

.

________________________________________

ExecuteReader: Connection property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.

Source Error:

Line 22: conn.Connection = Connection

Line 23: conn.CommandText = sql

Line 24: Return conn.ExecuteReader

Line 25: End Function

Line 26: Public Shared Function dispose()

Source File: c:\inetpub\wwwroot\login\DAL.vb Line: 24

Stack Trace:

[invalidOperationException: ExecuteReader: Connection property has not been initialized.]

System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean executing)

System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)

System.Data.SqlClient.SqlCommand.ExecuteReader()

login.DAL.excuteSql(String sql) in c:\inetpub\wwwroot\login\DAL.vb:24

login.SignOn.login(String username, String password) in c:\inetpub\wwwroot\login\SignOn.aspx.vb:41

login.SignOn.butSignOn_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\login\SignOn.aspx.vb:123

System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

System.Web.UI.Page.ProcessRequestMain()[/color

ولكن طلعت لي مشكلة أخرى كما ذكرت في آخر رد عند كتابة السطر هذا System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, False)

في Function login بدل السطر Response.Redirect("Register.aspx")

مع أني أضفت صفحة باسم default.aspx ولكن يطلع نفس الـ Error وهو

Server Error in '/login' Application.

--------------------------------------------------------------------------------

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested Url: /login/default.aspx

لا أدري ما المشكلة؟؟ :(

#14
إسماعيل عنجريني كتب:
الصفحة Default.aspx غير موجودة ضمن المجلد login.aspx وهذا هو سبب الخطأ والله أعلم ...

#15

أشكرك أخوي إسماعيل حليت الـ Error لن أنسى مساعدتك لي..

هذا الموضوع مغلق.

مواضيع مشابهة