0

Database tables: Registration & Purchase (Using Unique ID ranged 1-10000 as primary key and foreign key connecting to two tables)

How can I put on unique ID to my Purchase tables when tourists with corresponding ID do the online purchase?

I am using visual studio to work out the asp.net. However, how can I store the clients Unique ID to identify their logging activities?

        string result = "0";
        OleDbConnection connection = new OleDbConnection();
        connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sim\Desktop\Web.accdb";
        connection.Open();
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = "select ID from Registration where Name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "'";

        string getValue = command.ExecuteScalar().ToString();
        if (getValue != null)
        {
            result = getValue.ToString();
        }
        Session.Add("UserID", result);

        command.CommandText = "select * from Registration where Name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "'";

        OleDbDataReader reader = command.ExecuteReader();
        int count = 0;

        while (reader.Read())
        {
            count = count + 1;
        }
        if (String.IsNullOrEmpty(TextBox1.Text))
        {
            MessageBox.Show("You have't input the Username.");
        }
        if (String.IsNullOrEmpty(TextBox2.Text))
        {
            MessageBox.Show("you havn't input the Password.");
        }
        if (count == 1)
        {   

            MessageBox.Show("Username and password is valid");
            connection.Close();
            connection.Dispose();
            Response.Redirect("Loginpage.aspx", true);
        }
        else
        {
            MessageBox.Show("Username or Password is not matched");
        }
    }
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47

1 Answers1

0

You can try something like this to get your Registration (client) ID :

public static string GetClientId()
{
    string result = "0";
    OleDbConnection connection = new OleDbConnection();
    connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\sim\Desktop\Web.accdb";
    connection.Open();
    OleDbCommand command = new OleDbCommand();
    command.Connection = connection;
    command.CommandText = "select ID from Registration where Name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "'";

    string getValue = command.ExecuteScalar().ToString();
    if (getValue != null)
    {
        result = getValue.ToString();
    }
    conn.Close();
    return result;
}

You can save that ID in the Session using :

Page.Session["UserID"] = GetClientId();

Then once the purchase is made, you can save it by retrieving that value using :

string id = Page.Session["UserID"];

Finally, when a purchase is made, you will need to INSERT it in the database. To learn how to do this, you can look at this : C# Inserting Data from a form into an access Database

Community
  • 1
  • 1
JP Tétreault
  • 600
  • 5
  • 15
  • Yes that's my ideas. However, i want to know how to Build a session for the logged users to mark down their ID into Purchase tables. I don't have the ideas... – chaintao wong Apr 17 '15 at 10:59
  • Ok I added more details in my answer – JP Tétreault Apr 17 '15 at 11:12
  • i am so new... can i ask what is the meaning of Getvalue exist? and Page.Session["UserID"] = GetClientId(); <-- the UserID is the fields name of Purchase? – chaintao wong Apr 17 '15 at 11:16
  • getValue is just a variable holding the result of your query. ExecuteScalar is the important keyword in this code because it allows to get a single value instead of a row/table. About Page.Session, I suggest reading : https://msdn.microsoft.com/en-us/library/ms178581%28v=vs.140%29.aspx – JP Tétreault Apr 17 '15 at 11:20
  • can u help me see the above code?? how can i use two command.commandtext ? – chaintao wong Apr 17 '15 at 12:13
  • Each time you define a CommandText, you should call ExecuteScalar or ExecuteNonQuery to execute that command. So to use 2 commands, you will need to define a command, execute it, define another, execute it. – JP Tétreault Apr 17 '15 at 22:24
  • Session["UserId"] = Purchase table "Unique no." fields . how can i make it? and also should i use the public static ? whether i can use the variable in the same project (visual studio) other page? – chaintao wong Apr 18 '15 at 04:11