problem with the C# code

YashuAsha

YashuAsha

@yashuasha-I5vI1n Oct 25, 2024
Hello,

I am new to C#. i have written a function (method) in C# to connect with the SQL Database. if the UserID and Designation is true then it has to go for appropriate aspx page in C# but i am not getting how to call that method
i have table Users which consists of UserID(datatype as varchar) ,Password (datatype is varchar) and Role (datatype as varchar)

here is the code

namespace MyKlaus
{
public class AllFunctions
{
public static object DBconnection(string UserID, string Password)
{
try
{
SqlConnection con = new SqlConnection(@"Data Source=localhost\sqlexpress;Initial Catalog=klauDB;Trusted_Connection=true");


SqlCommand cmd = new SqlCommand("SELECT Username,Password from Login where Username='" + UserID + "' and Password='" + Password + "'", con);
con.Open();

SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
HttpContext.Current.Response.Redirect("HomePage.aspx");

}

else
{
HttpContext.Current.Response.Write("<script type='text/javascript'>alert('Login Failed!')</script>");
HttpContext.Current.Response.Flush();

}
con.Close();
}
catch
{
return null;
}

}


}

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Neeraj Sharma

    Neeraj Sharma

    @neeraj-iAaNcG Aug 1, 2012

    Can you please post in your code so that we can have a look at the issue more clearly
  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Aug 2, 2012

    try using Convert.ToBoolean(...)
    this is a wild guess from your question,
    if this doesnt work, kindly post the code which you think causes the problem.
  • YashuAsha

    YashuAsha

    @yashuasha-I5vI1n Aug 2, 2012

    Nick_Sharma
    Can you please post in your code so that we can have a look at the issue more clearly
    thank you, I have made mistake in giving the parameters this error (cannot implicitly convert int into bool) is solved.

    i have posted the code. i am not getting how to call DBconnection function. Can anyone help me
  • YashuAsha

    YashuAsha

    @yashuasha-I5vI1n Aug 2, 2012

    Also i am getting error as DBconnection(string, string) not all code paths returns a value
  • simplycoder

    simplycoder

    @simplycoder-NsBEdD Aug 4, 2012

    You are missing some return statement.
    Please Post the relevant code.