iT邦幫忙

0

sqlconnection NullReferenceException

GJ 2017-03-29 18:19:255683 瀏覽

在寫ASP.NET 遇到此問題一直找不到原因(眼殘看不出錯誤)/images/emoticon/emoticon06.gif
錯誤卡在 Dim cn As New SqlClient.SqlConnection(WebConfigurationManager.ConnectionStrings("MConnectionString").ConnectionString.ToString())
NullReferenceException 並未將物件參考設定為物件的執行個體

請問是哪裡少寫了嗎?

WEN.CONFIG

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <location path="Register.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

  <system.web>
    <authorization>
      <deny users="?"/>
    </authorization>
  </system.web>

  <connectionStrings>
    <add name="HConnectionString" connectionString="Data Source=SQL2;Initial Catalog=Ep;Persist Security Info=True;User ID=sa;Password=tEST" providerName="System.Data.SqlClient" />
    <add name="MConnectionString" connectionString="Data Source=SQL1;Initial Catalog=OPT;Persist Security Info=True;User ID=sa;Password=TTQQ" providerName="System.Data.SqlClient" />
  </connectionStrings>  
</configuration>

ASPX.VB

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            
        Dim cn As New SqlClient.SqlConnection(WebConfigurationManager.ConnectionStrings("MConnectionString").ConnectionString.ToString())

        Dim dr As SqlDataReader
        Dim sqlstr As String

        Try
            cn.Open()
            sqlstr = "select MAX(ID) FROM PROJREPT  "

            Dim cmd As New SqlCommand(sqlstr, cn)

            dr = cmd.ExecuteReader

            If dr.HasRows Then
                dr.Read()
                TextBox4.Text = dr.Read().ToString
            Else
                Response.Write("<script>alert('不可進入!');</script>")
            End If

            cmd.Cancel()
            dr.Close()

        Catch ex As SqlException
            Response.Write("<b>Error Message----  </b>" + ex.ToString() + "<HR/>")
        End Try

        cn.Close()
        cn.Dispose()
    End Sub
GJ iT邦研究生 5 級 ‧ 2017-03-31 13:15:46 檢舉
已自行解決~
重開新專案已OK

尚未有邦友回答

立即登入回答