SQL数据没有填充在asp webform中

时间:2017-03-25 19:49:15

标签: asp.net vb.net

目前无法获取任何SQL数据来填充Web表单。当我在Visual Studio预览模式下运行报表时,数据会填充,但是当通过我的Web应用程序运行时,它不会显示任何内容。

Imports System.Data.SqlClient
Public Class topvendors
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            getEntity()
            ' startdate.Text = DateTime.Today.ToShortDateString
        End If
    End Sub
    Protected Sub getEntity()
        Dim strConnString As String = Session("strconnection") 'ConfigurationManager.ConnectionStrings("TrialConnectionString").ConnectionString
        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "pGetProjectsByUser"
        cmd.Parameters.Add("@employeeid", SqlDbType.Int).Value = Session("userid")
        cmd.Parameters.Add("@reportid", SqlDbType.Int).Value = Request.QueryString("id")
        cmd.Connection = con

        Try
            con.Open()
            Dim dt As New DataTable()
            Dim ds As New DataSet()
            'Dim db As New SqlDataAdapter(cmd)

            Dim da = New SqlDataAdapter(cmd)
            da.Fill(ds)
            'db.Fill(ds)
            division.DataSource = ds.Tables(0)
            division.DataTextField = "entityname"
            division.DataValueField = "entitycode"
            division.DataBind()
            '  If ds.Tables(0).Rows.Count > 1 Then
            'divisions.Visible = False
            ' communities.DataSource = ds.Tables(1)
            'communities.DataTextField = "Projectname"
            ' communities.DataValueField = "projID"
            ' communities.DataBind()
            ' Else
            ' divisions.Text = ds.Tables(0).Rows(0)("entityname").ToString
            'division.Visible = False

            ' communities.DataSource = ds.Tables(1)
            'communities.DataTextField = "Projectname"
            'communities.DataValueField = "projID"
            'communities.DataBind()
            'End If

        Catch ex As Exception
            Throw ex
        Finally

            con.Close()
            con.Dispose()
        End Try
    End Sub
    Protected Sub submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles submit.Click
        ' Dim communities As String = Server.UrlEncode(Request.Form("communities"))
        ' Response.Write(communities)
        ' Dim removedays As Integer = Request.Form("removedays")
        'getEntity()
        ' MsgBox(sort.SelectedValue)
        Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder
        builder = New SqlConnectionStringBuilder(Session("strconnection"))

        Dim databasename As String = builder.InitialCatalog
        Dim dataname As String
        If databasename = "G3Live" Then
            dataname = "2fLiveReports"

        Else
            dataname = "2fTestReports"
        End If
        Response.Redirect("http://g3reports.danryanbuilders.com/ReportServer/Pages/ReportViewer.aspx?%" + dataname + "%2fPurchasing%2flance_tutorial&rs:Format=Excel&rs%3aCommand=Render&entityid=" & division.SelectedValue & "&begindate=" + begindate.Text + "&enddate=" + enddate.Text + "&sort=" + DropdownList1.SelectedValue)

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim builder As New System.Data.SqlClient.SqlConnectionStringBuilder
        builder = New SqlConnectionStringBuilder(Session("strconnection"))



        Dim databasename As String = builder.InitialCatalog
        Dim dataname As String
        If databasename = "G3Live" Then
            dataname = "2fLiveReports"

        Else
            dataname = "2fTestReports"
        End If
        Response.Redirect("http://g3reports.danryanbuilders.com/ReportServer/Pages/ReportViewer.aspx?%" + dataname + "%2fPurchasing%2flance_tutorial&rs:Format=PDF&rs%3aCommand=Render&entityid=" & division.SelectedValue & "&begindate=" + begindate.Text + "&enddate=" + enddate.Text + "&sort=" + DropdownList1.SelectedValue)

    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

  

注意:您在Session中存储连接字符串,不建议使用。你可以这样做但是你应该使用web.config更好   用于存储连接字符串的文件。

     

您可以参考: Avoid storing connection string in session for different sql schema

对于我推荐的解决方案

  1. 在获取连接字符串的值之前,必须先检查会话变量。
  2.   

    '检查会话是否为空

    If Not (Session("strconnection ") Is Nothing) Then
    {
         //
    }
    

    当它为Null或Nothing或Empty时,尝试再次检索连接字符串。

    1. 您需要在使用之前将会话变量强制转换为正确的类型,如
    2.   

      会话(" strconnection&#34)

      将其替换为

      Session("strconnection").toString()
      

        

      在...的帮助下演示会话("用户ID")和Request.QueryString(" id")   Integer.Parse / Integer.TryParse

      你可以阅读这个概念: Return Value From Session