title: Connect C# app with SQL Server 2008 via network
body: <p>I have an error when attempting to connect</p>
  
  <blockquote>
    <p><em>object reference not set to an instance of an object.</em></p>
  </blockquote>
  
  <p>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 (<code>Sharkawy-PC</code>) via network I need help for <code>app.config</code> code &amp; code to every form to use the connection from <code>app.config</code>.</p>
  
  <pre><code>    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();
      }
  </code></pre>
  
  <p>This is my connection in app.config</p>
  
  <pre><code>&lt;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" /&gt;
  </code></pre>