CrazyEngineers
  • problem with the C# code

    YashuAsha

    Member

    Updated: Oct 25, 2024
    Views: 922
    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;
    }

    }


    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Neeraj Sharma

    MemberAug 1, 2012

    Can you please post in your code so that we can have a look at the issue more clearly
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberAug 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.
    Are you sure? This action cannot be undone.
    Cancel
  • YashuAsha

    MemberAug 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
    Are you sure? This action cannot be undone.
    Cancel
  • YashuAsha

    MemberAug 2, 2012

    Also i am getting error as DBconnection(string, string) not all code paths returns a value
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberAug 4, 2012

    You are missing some return statement.
    Please Post the relevant code.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register