0

I've tried a million different methods of achieving this yet found none that work. Scroll down to bottom. (Note, this is a login form using a SQL Server database)

DatabaseConnection.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AllIn1Database
{
    class DatabaseConnection
    {
        private string sql_string;
        private string strCon;
        System.Data.SqlClient.SqlDataAdapter da_1;
        public string Sql
        {
            set
            {
                sql_string = value;
            }
        }
        public string connection_string
        {
            set { strCon = value; }
        }
        public System.Data.DataSet GetConnection
        {
            get { return MyDataSet(); }
        }
        private System.Data.DataSet MyDataSet()
        {
            System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
            con.Open();
            da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
            System.Data.DataSet dat_set = new System.Data.DataSet();
            da_1.Fill(dat_set, "Table_Data_1");
            con.Close();
            return dat_set;
        }
    }
}

Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace AllIn1Database
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        DatabaseConnection objConnect;
        string conString;
        DataSet ds;
        SqlConnection con = new SqlConnection();

        private void tnLogin_Click(object sender, EventArgs e)
        {
            try{
                System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
                con.Open();
                String cmp = "SELECT ISNULL(stUsername, '') AS stUsername, ISNULL(stPassword,'') AS stPassword, ISNULL(stRole,'') AS stRole FROM LoginDetails WHERE stUsername='" + txtUname.Text + "' and stPassword='" + txtPass.Text + "'";
            SqlCommand cmd = new SqlCommand(cmp, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
                SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())  
                {  
                string userText = txtUname.Text;
                string passText = txtPass.Text;
                        if (this.CompareStrings(dr["Name"].ToString(), userText) &&   
                       this.CompareStrings(dr["Password"].ToString(), passText)){
                           MessageBox.Show("Logged on as" + txtUname.Text);
        }
                        else
                        {
                            MessageBox.Show("Invalid Logon Details.");
                        }
                        dr.Close();
                        con.Close();
            }
            }
                catch (Exception err) {
                    MessageBox.Show(err.Message);
                }
            }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                objConnect = new DatabaseConnection();
                conString = Properties.Settings.Default.DatabaseConnectionString;
                objConnect.connection_string = conString;
                objConnect.Sql = Properties.Settings.Default.SQL;
                ds = objConnect.GetConnection;
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }

        private bool CompareStrings(string string1, string string2)
        {
            return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
        }
    }
}

The error occurs in the line

System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);

The error says

strCon does not exist in this context.

How would I make this successfully connect without throwing this error?

3
  • 1
    What is the value of strCon in your case? Also: you should use parametrized queries to avoid SQL injection attacks and you should NOT concatenate together your SQL statements as strings! Commented Dec 8, 2013 at 10:07
  • The database connection string. public string connection_string { set { strCon = value; } } Commented Dec 8, 2013 at 10:08
  • is it not constring instead of strCon you should use? you don't define strcon in your entire code Commented Dec 8, 2013 at 10:24

2 Answers 2

1

You're instantiating a connection without a connectionstring. And I do not see you setting it after the line:

System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();

Assuming your connection string is in Properties.Settings.Default.DatabaseConnectionString, you would do something like:

System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Properties.Settings.Default.DatabaseConnectionString);
Sign up to request clarification or add additional context in comments.

Comments

0

You don't have a connection string / havn't given strCon a value, you would need to do something like:

strCon = "myConnectionString";

to find out what the connection string is for you can use this website:

http://www.connectionstrings.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.