Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:26:05+00:00 2026-06-12T14:26:05+00:00

I have an error when attempting to connect object reference not set to an

  • 0

I have an error when attempting to connect

object reference not set to an instance of an object.

The app is working on my laptop with SQL Server database, but I need to connect the app on my laptop and connect with SQL Server on my PC (Sharkawy-PC) via network I need help for app.config code & code to every form to use the connection from app.config.

    private void btnlogin_Click(object sender, EventArgs e)
    {
        SqlCommand cmd;
       string UserName = txtusername.Text;
      var Connectionstring = ConfigurationManager.ConnectionStrings["BookingConnectionString"].ConnectionString;
      SqlConnection conn = new SqlConnection(Connectionstring);

       //////SqlConnection conn = new SqlConnection();
       //////conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["BookingConnectionString"];

        string strsql = "select * from UserInfo,UsersGroup  where UserInfo.GroupID=UsersGroup.GroupID and UserName = '" + UserName + "' and UserPassword='" + txtusername.Text + "'";
        cmd = new SqlCommand(strsql, conn);
        conn.Open();

        dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            dr.Read();
            string d = dr.GetString(9).ToString();

            if (d == "Admin")
            {
                AdminMainfrm adminmainfrm = new AdminMainfrm(txtusername.Text);
                this.Hide();
                adminmainfrm.ShowDialog();
            }
            else
            {
                UserMainfrm UserMainfrm = new UserMainfrm();
                this.Hide();
                UserMainfrm.ShowDialog();
            }
        }
        else
        {
            label5.Text = "Invalid Username or password, please try again";

        }
        conn.Close();
    }

This is my connection in app.config

<add name="Booking.Properties.Settings.BookingConnectionString"
     connectionString="DRIVER=SQL Server;SERVER=SHARKAWY-PC;UID=sa;PWD=123456;APP=Microsoft® Windows® Operating System;WSID=SHARKAWY;DATABASE=Booking;Network=DBMSSOCN"          
     providerName="System.Data.SqlClient" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:26:06+00:00Added an answer on June 12, 2026 at 2:26 pm

    One thing you’re going to need to do is change this line:

    var Connectionstring = ConfigurationManager.ConnectionStrings["BookingConnectionString"].ConnectionString;
    

    to:

    var Connectionstring = ConfigurationManager.ConnectionStrings["Booking.Properties.Settings.BookingConnectionString"].ConnectionString; 
    

    That should fix the object reference error.


    The next thing I’m going to recommend is that you simplify your connection string, you don’t need all of that information, just use something like this:

    <add name="Booking.Properties.Settings.BookingConnectionString"
         connectionString="SERVER=SHARKAWY-PC;UID=sa;PWD=123456;DATABASE=Booking"
         providerName="System.Data.SqlClient" />
    

    The next thing I’m going to recommend is that you do not use the sa account even during testing, if you get used to it you’re going to deploy it that way.

    The final thing I’m going to recommend is that you use parameterized queries to protect against SQL injection, so change your query logic from this:

    string strsql = "select * from UserInfo,UsersGroup  where UserInfo.GroupID=UsersGroup.GroupID and UserName = '" + UserName + "' and UserPassword='" + txtusername.Text + "'";
    cmd = new SqlCommand(strsql, conn);
    conn.Open();
    
    dr = cmd.ExecuteReader();
    

    To this:

    string strsql = "select * from UserInfo,UsersGroup  where UserInfo.GroupID=UsersGroup.GroupID and UserName = @UserName and UserPassword=@UserPassword";
    
    cmd = new SqlCommand(strsql, conn);
    cmd.AddParameterWithValue("@UserName", UserName);
    cmd.AddParameterWithValue("@UserPassword", txtusername.Text);
    
    conn.Open();
    
    dr = cmd.ExecuteReader();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have getting a javascript error when attempting to reference an object in a
I am attempting to connect to a SQL Server 2008 R2 instance from another
I have a server attempting to connect to a MySQL database hosted outside of
When attempting to connect to a SQL Server 2008 Instance using Management Studio, I
I am attempting to connect to a database I created in SQL Server 2008
I get the following error when attempting to connect to the MySQL database on
I have a database on line with Godaddy (who uses SQL Server 2005). They
FYI: SQL Server 2005 I have a database user account (user_web) that has the
Using Java, I get this error when attempting to connect to a mysql database:
I'm working on a C# application that queries a SQL Server 2008 Express instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.