简体   繁体   中英

Connect C# app with SQL Server 2008 via network

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" />

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();

Please consult the SQL Server Configuration Manager under SQL Server Network Configuration you will find the protocolls. Make sure that TCP/IP is enabled. After the initial installation this is usually disabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM