Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I keep retrieve my unique identifier as 00000000-0000-0000-0000-000000000000 .

How do I retrieve my unqiue identifier from my db.

Is this correct?

Guid code = Guid.Empty; I have a method to retrieve my code from my db

DAL

C#
public bool getPin(Guid pin)
{
    bool found = false;

    string queryStr = "SELECT Pin FROM [Account] WHERE Name = @name";

    SqlConnection conn = new SqlConnection(_connStr);
    SqlCommand cmd = new SqlCommand(queryStr, conn);


    cmd.Parameters.AddWithValue("@name", _name);

    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        pin = (Guid)dr["ActivationCode"];
        name = dr["Name"].ToString();
        found = true;
    }
    conn.Close();
    dr.Close();
    dr.Dispose();

    return found;
}

BLL
C#
public Guid FindPin(Guid pin)
{
    string returnMessage = "";

    if (pin == null)
    {
        Account account = new Account();
        bool found = account.getPin(pin);
        if (found == true)
        {
            returnMessage += "Your pin is wrong! <br />";

        }

    }
    return pin;
}

Presentation Layer

C#
AccountBLL ac = new AccountBLL();

Guid pin = Guid.Empty;

Guid pin1 = ac.FindPen(pin)


[Edit - CHill60]Added the question back in from previous version of post[/Edit]
Posted
Updated 8-Jul-14 5:37am
v4
Comments
OriginalGriff 8-Jul-14 11:03am    
Without your code, we can't tell what is wrong - we would need to see both the insert and select code to be sure of what the problem is.
Use the "Improve question" widget to edit your question and provide better information.
Bernhard Hiller 9-Jul-14 4:27am    
There's more wrong with your code:
string queryStr = "SELECT Pin FROM [Account] WHERE Name = @name";
will lead to an exception at
name = dr["Name"].ToString();

1 solution

Look at the lines
Guid pin = Guid.Empty;
 
Guid pin1 = ac.FindPen(pin)

Firstly you have a spelling error in there, but importantly you are passing Guid.Empty into your FindPin method.

Within that method you only try to look something up
VB
if (pin == null)
Well it's not null, it's Guid.Empty!

Try
VB
if (pin == Guid.Empty)
 
Share this answer
 
Comments
Good catch. Take a 5. :)
polkj 8-Jul-14 11:59am    
How should I declare my guid variable at presentation tier?
CHill60 8-Jul-14 12:44pm    
You are declaring it correctly ... Guid is a non-nullable type, so checking pin == null is meaningless in the FindPin method.
polkj 8-Jul-14 12:50pm    
I declare wrongly. In the presentation tier I need to retrieve the pin from my db. So cannot be
Guid pin = Guid.Empty; If the pin macth with my database value, repsonse.redirect else show lable msg
CHill60 9-Jul-14 3:22am    
How else can you declare it?
If you don't assign a value of Guid.Empty then you will get an error "Use of unassigned local variable 'pin'" and you can't assign it to null because Guid is non-nullable. You can still test it against your database.
The rest of your logic doesn't look right though - where are you getting the pin to match against your database?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900