I hope you can help me with the following code that I have in visual studio, I am programming a user login, in asp.net with c # and it turns out that in my aspx form called PortalAlumons.aspx.cs I wrote the following code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection SqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectSQL"].ToString());
SqlCon.Open();
string query = "SELECT * FROM sisacad.USUARIO WHERE Rut_Nro = '"+Txt_RutNro.Text+"' AND Rut_Dig = ' "+Txt_RutDigito.Text+" ' AND Contrasena = ' "+Txt_Pass.Text+" ' ";
SqlCommand cMD = new SqlCommand(query, SqlCon);
string output = cMD.ExecuteScalar().ToString(); //ESTA LINEA ME ARROJA EL ERROR
if (output == "1")
{
Session["user"] = Txt_RutNro.Text;
Response.Redirect("~/Alumno.aspx");
}
else
{
Response.Write("Conexion Fallida");
}
}
It turns out that the error that throws me says the following:
Object reference not set as an instance of an object.
Description: Unhandled exception when executing the current Web request. Check the stack trace for more information about the error and where it originated in the code.
Exception details: System.NullReferenceException: Object reference not established as an instance of an object.
Source code error:
Line 23:
Line 24: SqlCommand cMD = new SqlCommand (query, SqlCon);
Line 25: string output = cMD.ExecuteScalar (). ToString ();
Line 26:
Line 27: if (output == "1")
This happens to me when I click on a link that redirects me to the page of the student portal, where the student will be able to log in and later to enter his personal study website. And the error line is 25.
If you could help me, I would be very grateful, thank you.