I am creating a simple Hospital Management System, and I was having a problem connecting to the database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
SqlConnection connectionstring = new SqlConnection("Server=.\\SQLEXPRESS;Database=TestDB;User Id=test; Password = woooow; ");
protected void Button1_Click1(object sender, EventArgs e)
{
string cmdText = "SELECT 1 FROM Login WHERE Username = '" + TextBox1.ToString() + "' AND Password = '" + TextBox2.toString()+ "'";
// using (SqlConnection cnn = new SqlConnection("Server=.\\SQLEXPRESS;Database=TestDB"))
using (SqlCommand SelectCommand = new SqlCommand(cmdText, connectionstring))
{
SqlDataReader myReader;
connectionstring.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
//open form
}
else
{
}
}
}
}
this is the code I use for the Login form in a normal C# application. but looks like I am doing something wrong in TextBox1.toString() and TextBox2.toString(). How can I take the exact value of the textbox? by googling around, I saw many posts which say it, but everything is different from each other and making me really confused about it.
So, which is the best way to do that?
Thanks.