Someone help me , new to this language :)

This is the connection string in my Login Screen and im having a Hard time on how my Main Screen can populate the fields about the information of that username . .

    `   cmd = new SqlCommand("select * from Profiles where [email protected] AND [email protected] ",conn);
        cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
        cmd.Parameters.AddWithValue("@Password", txtPass.Text);
        var adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ds);
        sMessage = cmd.CommandText;
        if(ds.Tables[0].Rows.Count > 0 )
        {

            var primaryPages = new Intent(this, typeof(primary));
             StartActivity(primaryPages);

        }`

will i declare another connection string in my Main page?
i tried using "Session" but its not working

thanks !

Posts

  • IvanHarrisIvanHarris USMember ✭✭✭

    Yes, you will declare another connection string on your Main page. Session doesnt work on Android. You will need to either use preferences to save the users Id or pass it via the Intent.

    Ive attached a class that I created to show you what I do. I put all my SQL methods into this SQL class. Then I just declare the class and pass the information to it. It saves space when trying to debug. :)

    In this case, in your Login activity, you would do this:

    string[] LoginInfo = { txtUsername.Text.Trim(), txtPass.Text };
    SQL sqlClass = new SQL();
    sqlClass.OpenConn(); //OPENS YOUR DB CONNECTION
    bool LoginValid = sqlClass.CheckLogin(LoginInfo); //CHECKS IF USER IS VALID
    sqlClass.CloseConn(); //CLOSES YOUR DB CONNECTION
    
    if (LoginValid == true)
    {
        //PUT UserID to settings (OR you could pass it through the intent to your Main activity)
        var settings = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
        var editor = settings.Edit();
        editor.PutInt("UserID", Convert.ToInt32(GetUserID(txtUsername.Text.Trim(), txtPass.Text)));
        editor.Commit();
        var primaryPages = new Intent(this, typeof(primary));
        StartActivity(primaryPages);
    }
    else
        Toast.MakeText(this, "Login Failed! Please try again.", ToastLength.Long).Show();
    

    Within your Main activity:

    int UserID = 0;
    var settings = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
    UserID  = settings.GetInt("UserID", 0);
    SQL sqlClass = new SQL();
    sqlClass.OpenConn(); //OPENS YOUR DB CONNECTION
    string[] UserInfo = sqlClass.GetUserInformation(UserID); //GETS USER INFORMATION FROM DB
    sqlClass.CloseConn(); //CLOSES YOUR DB CONNECTION
    string Username= UserInfo[0];
    string Password = UserInfo[1];
    string FirstName = UserInfo[2];
    string LastName = UserInfo[3];
    

    Does that help explain your question?

  • NeuraSNeuraS HKMember

    But the one that i am using in Main Activity will be same as the one you stated in your Text file?

    Thanks!

  • NeuraSNeuraS HKMember

    if you declared a class for sql connection. . .how do you call that class :) Ty!

  • NeuraSNeuraS HKMember
    edited April 2013

    how can i solve this line .
    .
    .

    editor.PutInt("UserID", Convert.ToInt32(GetUserID(txtUsername.Text.Trim(), txtPass.Text)));

  • NeuraSNeuraS HKMember

    got error on GetUserID . Cannot exist in current context

  • NeuraSNeuraS HKMember

    Sir im already done with my GetUserID Error . But now

    SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { id = Convert.ToInt32(sdr["applicant_Username"]); }

    Its going on catch instead of While

    Youre the one that can help me!

  • YamilBrachoYamilBracho VEMember

    And the exception is ...?

  • IvanHarrisIvanHarris USMember ✭✭✭

    What background do you have in programming? Java?

    To add the class I created for you, do this (within Visual Studio <--im assuming you're using this):

    1. Right click on your project
    2. Click Class. <-- this will create a new class for you
    3. Name it SQL.cs
    

    Now that you have your new class created

    1. open the txt file I created for you. 
    2. Replace all the _using _in your new class with the _using _in the txt file.
    3. Copy from the txt file from: public class SQL all the way to the closed bracket (**}**).
    4. replace "class SQL { }" within your newly created SQL.cs file with the "public class SQL {....}" that you just copied from the txt file.
    

    Now that you have the SQL.cs file completed. Go back to your Main activity.
    and put this code in it.

    int UserID = 0; //declares INT to hold your users Id
    var settings = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext); //Initiates PreferenceManager in order to get/set preferences
    UserID  = settings.GetInt("UserID", 0); //Gets preference "UserID" from the preferences
    SQL sqlClass = new SQL(); //Initiates the SQL class you just created (so you can use the methods within it
    sqlClass.OpenConn(); //OPENS YOUR DB CONNECTION
    string[] UserInfo = sqlClass.GetUserInformation(UserID); //GETS USER INFORMATION FROM DB
    sqlClass.CloseConn(); //CLOSES YOUR DB CONNECTION
    string Username= UserInfo[0]; //Takes the data from the array and puts it into a string.
    string Password = UserInfo[1]; //Same above.
    string FirstName = UserInfo[2]; //Same above.
    string LastName = UserInfo[3]; //Same above.
    

    To answer your question about the GetUserID, Im sorry. It should have been like this:

    editor.PutInt("UserID", Convert.ToInt32(sql.GetUserID(txtUsername.Text.Trim(), txtPass.Text)));
    //with the sql. right before it.
    

    With regards to the catch question; Yamil is right. What is the exception? It could be a number of things: sql connection not opened, no such column as applicant_Username within table. Your SQL query could be throwing the exception.

  • NeuraSNeuraS HKMember
    edited April 2013

    my problem is that it never passes at the while statement and go directly in catch

    SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { id = Convert.ToInt32(sdr["applicant_Username"]); }

  • IvanHarrisIvanHarris USMember ✭✭✭

    Yeah, your while could have a couple problems: sql connection is not opened, applicant_Username is not a column within your table or your applicant_Username is a column but is NOT an INT. Could be anything.

  • NeuraSNeuraS HKMember

    sir @ivan id value is always 0

    When will it be 1

    My Username is string. . and its always going on the catch. . maybe theres an error

    ' while (sdr.Read())
    {
    id = Convert.ToInt32(sdr["Username"]);
    }'

  • NeuraSNeuraS HKMember

    and what is the _id here in SQL.cs?
    :)

  • NeuraSNeuraS HKMember

    @ivan Sir . . can you help me . . because there's an instance where it have 3 usernames but different password. . Thanks. . And i always getting error on Convert.ToInt32 :) Thank U Very much!

  • IvanHarrisIvanHarris USMember ✭✭✭
    edited April 2013

    Ok, that's your problem. You are trying to convert your Username column, which is a string, to an integer.

    Do this:

    while (sdr.Read())
    {
      string Username = Convert.ToString(sdr["Username"]);
    }
    

    In the SQL.cs class, change the _id to your Id column. Then you can do this if you need:

    while (sdr.Read())
    {
      int Id = Convert.ToInt32(sdr["Id"]);
      string Username = Convert.ToString(sdr["Username"]);
    }
    

    If you want to catch your errors and not have the app crash every time there is a problem, you can do this:

    try
    {
      //Code to try
      while (sdr.Read())
      {
        int Id = Convert.ToInt32(sdr["Id"]);
        string Username = Convert.ToString(sdr["Username"]);
      }
    }
    catch
    {
      Log.Warn("BROKEN", "Code I just tried is BROKEN!");
    }
    

    What other programming languages do you know?

  • IvanHarrisIvanHarris USMember ✭✭✭

    I sent you a private message with my email. Email me your code so I can better help you.

Sign In or Register to comment.