Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created a function to retrieve a value based on the session.

The function will take the session value and return the value I want

The problem is : it works with some rows, but it's not with others

The table I have contains more than 2000 rows

This is my function.



HTML
Public Shared Function GetUserNational(ByVal NationalID As Integer) As Int64
        Const sql = "SELECT Applicant_ID FROM DIP_Applicant where NationalID = @NationalID"
        Using con As New SqlConnection(ConfigurationManager.ConnectionStrings("mydbConnectionString").ConnectionString)
            Using cmd = New SqlCommand(sql, con)
                cmd.Parameters.AddWithValue("@NationalID", NationalID)
                con.Open()
                Using reader = cmd.ExecuteReader()
                    If reader.HasRows Then
                        reader.Read()
                        Dim count As Int64 = reader.GetInt32(0)
                        Return count
                    End If
                End Using
            End Using
        End Using
    End Function





My table contain Applicant_Id type int and primary key contain also National_Id as nvarchar(50)

in my website the user should enter his or her national Id and i will return the applicant Id

the same call the same queriy

for example I have already national_Id "102323232" when I enter it in my text box it shows me the applicant Id

but when I give anouther Id like "100555666"; it return 0 but the value should not be zero-

it's already in the table and it's not 0
Posted

1 solution

You say it's in the table, but your code doesn't show that - either the first Applicant_ID in the table that matches a NationalID of 100555666 is zero, or there is no such record.

The first thing to do is use the debugger to step through the function and make sure that the reader.HasRows returns true - at least one row matches. If that is true, then look at the value in the reader and see what it says.

If it is zero, then you need to check your database and look at all the values that match the NationalID - I can only assume there is more than one Applicant from a single country...
 
Share this answer
 
Comments
Ali M Alramil 27-Apr-13 4:33am    
but the problem is I have Zero record with the value "0 " in my database
and it's not only the National Id I gave return 0 and mony other national Id's return the value I want
the national Id and the Applicant Id both are not repeted

^_^
OriginalGriff 27-Apr-13 4:35am    
How did you check?
Ali M Alramil 27-Apr-13 4:38am    
I'm creating a website so I have checked it throug the page I created
I enter the national Id and then Click submit
the I should recieve the applicant Id
OriginalGriff 27-Apr-13 4:42am    
Not a good check - you need to use something like Sql Server Management Studio (or SSMS for short) to look at the raw data - the chances are that you are using very similar code to "prove" your data is right to that which you are testing!

Look at the raw table in SSMS - you will get a better idea of what is happening.
Ali M Alramil 27-Apr-13 4:45am    
I looked for the table and the record I checkd and there is no problem with it
is that what you mean,,, to see the data and check the answers in the 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