Results 1 to 5 of 5

Thread: SQL Connection prob from ASP [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    SQL Connection prob from ASP [Resolved]

    VB.NET 2003 and SQL 2000
    I am attempting to create a web form to query a SQL DB to post xray history using Windows authentication.
    Currently the test IIS is running on my XP PC and the SQL server is on the network (domain).

    The Form works fine when I run it from the VB Designer.
    When I run it from just a browser it fails the first time with a General Network Error. Check you Network documentation. If I try it again it works....I think because my code does not disconnect from the sql server even if I close the browser. When I get the initial error, I can look in the Ent Manager and see that I did get connected to the SQL server. I looked up the error on the Microsoft site and it says the SQL side is not configured for SSL...I don't have SSL configured on the IIS server. I do have the domain authentication turned on and the anonymous turned off in the IIS.

    So 2 questions.
    1). Why does my app get the Network Error and how do I fix it?
    2). How do I make the app close the connection to the SQL server?

    Here is my code:
    VB Code:
    1. Imports System.Data
    2. Imports System.Data.SqlClient
    3.  
    4. Public Class WebForm1
    5.     Inherits System.Web.UI.Page
    6.     'Protected WithEvents grdPatient As System.Web.UI.WebControls.DataGrid
    7.     'Declare a Connection object that is global in scope
    8.     Dim objConnection As SqlConnection
    9.  
    10.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.         'Put user code to initialize the page here
    12.         'Initialize the Connection Object...
    13.         objConnection = New SqlConnection("Server=TESTSQL1; Database=XRAY; Integrated Security=SSPI; Persist Security Info=False; Initial Catalog = XRAY; ") 'Trusted_Connection = True")
    14.     End Sub
    15.  
    16.     Private Sub btnFindPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindPatient.Click
    17.         'Declare Objects...
    18.         Dim objDataSet As DataSet
    19.         Dim objDataAdapter As SqlDataAdapter
    20.         Dim myCmd As SqlCommand
    21.         Dim myReader As SqlDataReader
    22.         Dim RsltMedRec As String
    23.         Dim RsltRad As String
    24.         Dim RsltPatient As String
    25.         Dim RsltSex As String
    26.         Dim RsltBirthDate As String
    27.         Dim RsltSocSec As String
    28.         Dim RsltLastExam As String
    29.         Dim strMed_REC As String
    30.  
    31.         'Assign the value in the input box to the query variable
    32.         strMed_REC = txtGetMedRec.Text
    33.  
    34.         'Create a SQL Command Object to query Patient by MedRec number
    35.         myCmd = objConnection.CreateCommand
    36.         myCmd.CommandText = "SELECT Med_Rec#, RAD#, First_Name," & _
    37.         "Last_Name, Sex, Birth_Date, Soc_Sec#, Last_Exam_Date " & _
    38.         "From tblPatient " & _
    39.         "WHERE Med_Rec#= '" & strMed_REC & "'"
    40.  
    41.         objConnection.Open()
    42.         myReader = myCmd.ExecuteReader()
    43.  
    44.         'Place the query Results into the result strings
    45.         Do While myReader.Read()
    46.             RsltMedRec = myReader.GetString(0)
    47.             RsltRad = myReader.GetString(1)
    48.             RsltPatient = myReader.GetString(3) & ", " & myReader.GetString(2)
    49.             RsltSex = myReader.GetString(4)
    50.             RsltBirthDate = myReader.GetString(5) & "   "
    51.             RsltSocSec = myReader.GetString(6)
    52.             RsltLastExam = myReader.GetString(7)
    53.         Loop
    54.  
    55.         'Display Results on the web form...
    56.         lblMedRec.Text = RsltMedRec
    57.         lblRad.Text = RsltRad
    58.         lblPatient.Text = RsltPatient
    59.         lblSex.Text = RsltSex
    60.         lblBirthDate.Text = RsltBirthDate
    61.         lblSocSec.Text = RsltSocSec
    62.         lblLastExam.Text = RsltLastExam
    63.         'Close(DataReader)
    64.         objConnection.Close()
    65.  
    66.         'Set the SQL Query String to pull all the procedures...
    67.         objDataAdapter = New SqlDataAdapter("SELECT Proc_Date_Time as 'Proc Date/Time'," & _
    68.         "Dept, PROC#, Proc_Description As 'Proc Desc'," & _
    69.         "Ordering_Phy_Last As 'Order Phy LastName'," & _
    70.         "Ordering_Phy_Firs As 'Order Phy FirstName'" & _
    71.         "FROM tblProcedure WHERE Med_Rec# = '" & strMed_REC & "'" & _
    72.         "Order By Proc_Date_Time DESC", objConnection)
    73.  
    74.         'Initialize the DataSet object and fill it...
    75.         objDataSet = New DataSet
    76.         objDataAdapter.Fill(objDataSet, "tblProcedure")
    77.  
    78.         'Declare a DataView Object, populate it, and sort the data in it...
    79.         Dim objDataView As DataView = objDataSet.Tables("tblProcedure").DefaultView
    80.         grdPatient.DataSource = objDataView
    81.         grdPatient.DataBind()
    82.         'Close Connection
    83.         objConnection.Close()
    84.     End Sub
    85. End Class

    The error is on line 79: MyReader = myCmd.ExecuteReader()
    TIA, I am a wooky, oops I mean rookie! - Jeff
    Last edited by rothjm; Jun 3rd, 2005 at 12:42 PM.

  2. #2
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Re: SQL Connection prob from ASP

    I think you are getting the error because you are trying to use windows integrated security. I use the sql user name and password. That way, it won't matter who is logged into the machine, the program will work.

    Here is an example of the sql connection that I use.
    VB Code:
    1. Dim cn As New SqlClient.SqlConnection()
    2.  
    3. cn.ConnectionString = "Integrated Security=True;" & _
    4. "Data Source=Server1;Initial Catalog=Pubs;" & _
    5. "user id=UserName;password=Password;"
    6. cn.Open()
    On your second question, cn.close will close the connetion. cn.dispose will completly remove it from memory. I do both.
    David Wilhelm

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: SQL Connection prob from ASP

    The bossman would really like to use the Windows Authentification...then if it fails use the SQL login.
    Anyone have any ideas for me to try?

    The error is on line 79: MyReader = myCmd.ExecuteReader()
    I also tried:
    myReader = myCmd.ExecuteReader(CommandBehavior.CloseConnection)

    I get the same error:
    Code:
    General network error. Check your network documentation. 
    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.Data.SqlClient.SqlException: General network error. Check your network documentation.
    
    Source Error: 
    
    
    Line 79: 
    Line 80:         objConnection.Open()
    Line 81:         myReader = myCmd.ExecuteReader(CommandBehavior.CloseConnection)
    Line 82: 
    Line 83:         'Place the query Results into the result strings
     
    
    Source File: c:\inetpub\wwwroot\XrayHistory\XrayHistory.aspx.vb    Line: 81 
    
    Stack Trace: 
    
    
    [SqlException: General network error.  Check your network documentation.]
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
       System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
       XrayHistory.WebForm1.btnFindPatient_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\XrayHistory\XrayHistory.aspx.vb:81
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1292



    TIA - Jeff
    Last edited by rothjm; May 25th, 2005 at 01:27 PM.

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: SQL Connection prob from ASP

    A little more info:
    I commented out the bottom query (Procedure) and the top query (Patient) runs just fine. When I comment out the top part I still get the error.
    Once it fails once...it runs fine because I am still logged into the SQL server.
    The problem has to be with the second query/connection/DataAdapter.

    Thanks for any insight!!!

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2004
    Location
    KS, USA
    Posts
    34

    Re: SQL Connection prob from ASP [Resolved]

    I am still getting the network error the first time I try to run the program...after it fails the first time it leaves my login connection on the sql server and then it works everytime (as long as I don't kill my connection).
    I condensed my code down to try to find the problem. I also changed to run a stored procedure. Also tried hard coding the username and password (same result). I think the problem is where I fill the dataset and bind to the grid.
    Can someone look at that section and see if there is anything wrong.
    Thanks for any advice!!!

    Code:
    Imports System.Data
    Imports System.Data.SqlClient
    'Imports System.Security
    
    Public Class WebForm1
        Inherits System.Web.UI.Page
        'Protected WithEvents grdPatient As System.Web.UI.WebControls.DataGrid
        'Declare a Connection object that is global in scope
        Dim objConnection As SqlConnection
    
    #Region " Web Form Designer Generated Code "
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            'Initialize the Connection Object...
            objConnection = New SqlConnection("Server=TESTSQL1\MISC; Database=XRAY; Integrated Security=SSPI; Persist Security Info=FALSE;")
            'objConnection = New SqlConnection("Server=TESTSQL1\MISC; Database=XRAY; uid = XX; pwd = XXXXXX;")
        End Sub
    
        Private Sub btnFindPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindPatient.Click
            'Declare Objects...
            Dim objDataSet As DataSet
            Dim objDataAdapter As SqlDataAdapter
            Dim myCmd As SqlCommand
            Dim myReader As SqlDataReader
    
            'Assign the value in the input box to the query parameter
            strMed_REC = txtGetMedRec.Text
    
            objDataAdapter = New SqlDataAdapter("sp_GetProcByMedRec", objConnection)
            objDataSet = New DataSet
            objConnection.Open()
            objDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
            objDataAdapter.SelectCommand.Parameters.Add("@MedRec", SqlDbType.Char).Value = strMed_REC
          
            objDataSet = New DataSet
            objDataAdapter.Fill(objDataSet, "Med_Rec#")
            grdPatient.DataSource = objDataSet.Tables("Med_Rec#").DefaultView
            grdPatient.DataBind()
    
            objConnection.Close() 'Close Connection
            objConnection.Dispose() 'Clears Memory        
        End Sub
    End Class
    Error:
    Code:
    General network error. Check your network documentation. 
    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.Data.SqlClient.SqlException: General network error. Check your network documentation.
    
    Source Error: Line 97
    
    
    Line 95: 
    Line 96:         objDataSet = New DataSet
    Line 97:         objDataAdapter.Fill(objDataSet, "Med_Rec#")
    Line 98:         grdPatient.DataSource = objDataSet.Tables("Med_Rec#").DefaultView
    Line 99:         grdPatient.DataBind()
    Last edited by rothjm; Jun 3rd, 2005 at 12:41 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width