Hi,
there, I need help with the connection string . An error saying 'Object reference not set to an instance of an object.'
Can be seen when i make the system to run. It effects the code below:

[B]Dim str As String = ConfigurationManager.ConnectionStrings("CourierSystemConnectionString").ConnectionString[/B]

Partial Class _Default
    Inherits System.Web.UI.Page
    [B]Dim str As String = ConfigurationManager.ConnectionStrings("CourierSystemConnectionString").ConnectionString[/B]    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim rdr As SqlDataReader
    Dim sql As String
    Dim t As String

    Protected Sub btnlogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnlogin.Click
        con = New SqlConnection(str)
        con.Open()
        Dim type As String
        type = ddltype.SelectedValue
        If type = "student" Then
            sql = "select StaffID, Password from StaffRegis_Table where uname='" + tuname.Text + "' and upass='" + tupass.Text + "'"
        Else
            sql = "select StaffID, Password from StaffRegis_Table where uname='" + tuname.Text + "' and upass='" + tupass.Text + "'"
        End If
        cmd = New SqlCommand(sql, con)
        rdr = cmd.ExecuteReader
        If rdr.Read Then
            If ddltype.SelectedValue = "student" Then
                Session("ses_dname") = rdr("sfname") + " " + rdr("slname")
                Session("ses_uid") = rdr("sid")
                Response.Redirect("mypage.aspx")
            Else
                Session("ses_dname") = rdr("stffname") + " " + rdr("stflname")
                Session("ses_uid") = rdr("stfid")
                Session("ses_utype") = rdr("utype")
                Response.Redirect("index.aspx")
            End If
        End If

    End Sub
End Class

Recommended Answers

All 12 Replies

You must not have this defined in your configuration file:

ConfigurationManager.ConnectionStrings("CourierSystemConnectionString")

You must not have this defined in your configuration file:

ConfigurationManager.ConnectionStrings("CourierSystemConnectionString")

Hi

Try to use
ConfigurationManager.AppSettings("KeyNameInYourWeb.Config")

there are two cases, either you've not written this connection string in your web.config..else

you are missing .toString() which i do generally in C# as..

string strConnection = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();

there are two cases, either you've not written this connection string in your web.config..else

you are missing .toString() which i do generally in C# as..

string strConnection = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();

Missing a .ToString() would not give you a null reference error, it would give you a type error.

Missing a .ToString() would not give you a null reference error, it would give you a type error.

thnx

thnx

Define this connection string in web.config file

<connectionStrings>
    <add name="CISConnectionString" connectionString="Data Source=abc;Database=UserInformation;Uid=sa;Pwd=ists;" providerName="System.Data.SqlClient"/>
   
  </connectionStrings>

Call it in code behind like this: string connString = ConfigurationManager.AppSettings["CISConnectionString"];

I understand what is required, but i am not able to as when i was installing my MSSQL, i only installed as Windows Authentication and not as SQL Server Authentication. In this manner, I didn't set any password or user id. please help

A connection string being undefined has nothing to do with MSSQL. You have a problem in your configuration file unless you have fixed your configuration file and are now unable to connect (and didn't tell us you solved the original problem) so I would stick to examining your configuration file.

Once your fix the null reference exception we can move on to getting you a valid connection string.

try using this <add key="ConnectionString" value="server=(local);Integrated Security=SSPI;database=abc" />

the 'value' attribute is not declared. This is what it says.

Post your app settings XML configuration file here. You have a problem with it.

Did u use this?
<add key="ConnectionString" value="server=(local);Integrated Security=SSPI;database=abc" />

define server=.
like this
value="server=.;Integrated Security=SSPI;database=abc" />

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.