Reputation: 9
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?
Upvotes: 0
Views: 755
Reputation: 1456
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);
Upvotes: 1
Reputation:
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/
Upvotes: 0