So, I am new to VS and C#, I am self-teaching to get a better understanding of the back-end of the product I work with. I have created a small database with some information and a Login form. Everything appears to compile correctly but is that the security way to do that or there is another way, any help is appreciated. Thanks.
private void button2_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from tbladmin where username=@username and password=@password", sqlcon);
cmd.Parameters.AddWithValue("@username", txtusername.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
try
{
if (dtbl.Rows.Count > 0)
{
if (dtbl.Rows[0]["role"].ToString() == "Admin")
{
SqlCommand cmd2 = new SqlCommand("select date from tbladmin where username=@username and password=@password", sqlcon);
cmd2.Parameters.AddWithValue("@username", txtusername.Text);
cmd2.Parameters.AddWithValue("@password", txtpassword.Text);
SqlDataAdapter sda2 = new SqlDataAdapter(cmd2);
DataTable dss = new DataTable();
sda2.Fill(dss);
String value2 = dss.Rows[0][0].ToString();
DateTime date = DateTime.Parse(dss.Rows[0][0].ToString());
Class1.Txtusername = txtusername.Text;
Debug.WriteLine("value is : " + value2);
if (date.AddDays(90) < DateTime.Now)
{
Changpassad obj2 = new Changpassad();
this.Hide();
obj2.Show();
}
else
{
calladmin obj = new calladmin(dss.Rows[0][0].ToString());
this.Hide();
obj.Show();
}
}
}
else if (dtbl.Rows.Count == 0)
{
SqlCommand cmd3 = new SqlCommand("select date from tblcallcenter where username=@username and password=@password", sqlcon);
cmd3.Parameters.AddWithValue("@username", txtusername.Text);
cmd3.Parameters.AddWithValue("@password", txtpassword.Text);
SqlDataAdapter sda2 = new SqlDataAdapter(cmd3);
DataTable dss = new DataTable();
sda2.Fill(dss);
String value2 = dss.Rows[0][0].ToString();
DateTime date = DateTime.Parse(dss.Rows[0][0].ToString());
Debug.WriteLine("value is : " + value2);
if (date.AddDays(90) < DateTime.Now)
{
Changpass obj2 = new Changpass()/;
this.Hide();
obj2.Show();
}
else
{
SqlCommand cmd4 = new SqlCommand("select user_id , username from tblcallcenter where username=@username and password=@password", sqlcon);
cmd4.Parameters.AddWithValue("@username", txtusername.Text);
cmd4.Parameters.AddWithValue("@password", txtpassword.Text);
SqlDataAdapter From_sda = new SqlDataAdapter(cmd4);
DataTable From_ds = new DataTable();
From_sda.Fill(From_ds);
String value1 = From_ds.Rows[0][1].ToString();
int id = int.Parse(From_ds.Rows[0][0].ToString());
Debug.WriteLine("value is : " + value1);
Class1.Txtusername = txtusername.Text;
this.Hide();
SqlCommand cmd5 = new SqlCommand("select [from], Take from tblcallcenter where username=@username and password=@password", sqlcon);
cmd5.Parameters.AddWithValue("@username", txtusername.Text);
cmd5.Parameters.AddWithValue("@password", txtpassword.Text);
SqlDataAdapter sda1 = new SqlDataAdapter(cmd5);
DataTable ds = new DataTable();
sda1.Fill(ds);
Callcenter1 obj = new Callcenter1(ds.Rows[0][0].ToString(), ds.Rows[0][1].ToString());
this.Hide();
obj.Show();
}
}
else
{
MessageBox.Show("Invalid Login try checking Useraname Or Password !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception)
{
MessageBox.Show("Invalid Login try checking Useraname Or Password !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}