locked
Get NULL Value and Replace it RRS feed

  • Question

  • Hi,

    Please help me with this.

    I have Database and there is column for application key.

    1) when vb application start i need to read this  column and if there is no record (NULL) then asking for enter Key Code. This is happen only when first time use this application.

    2) After Complete 1st step >>> I provide my key as string in source. then vb application check the source key is = database key 

    Database: SQLLocaldb

    Database: KDatabase.mdf

    Table: Kinfo

    Table Column: Kid

    Thank you.... 


    RohanD




    • Edited by RAT_SL Friday, May 31, 2019 12:25 AM
    Friday, May 31, 2019 12:18 AM

Answers

  • Hello,

    The following was done using a SQL-Server database on a server with a different database name but by changing the connection string there is zero difference as the table is the same name and column name is the same. 

    If Kid is not a string field then rather than using CStr(keyValue) use CInt(keyValue).

    Class for checking the key entered

    Imports System.Data.SqlClient
    
    Namespace Classes
        Public Class Operations
            Private ConnectionString As String =
                        "Data Source=KARENS-PC;" &
                        "Initial Catalog=NorthWindAzureForInserts;" &
                        "Integrated Security=True"
            Public Function GetApplicationKey(ByVal pApplicationKey As String) As String
                Dim result as String = ""
                Dim selectStatement = "SELECT Kid FROM dbo.Kinfo WHERE Kid = @Kid"
                Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                    Using cmd As New SqlCommand With {.Connection = cn}
                        cmd.CommandText = selectStatement
                        cmd.Parameters.AddWithValue("@Kid", pApplicationKey)
                        cn.Open()
                        Dim keyValue = cmd.ExecuteScalar()
                        If keyValue Is Nothing Then
                            result = ""
                        Else
                            result = CStr(keyValue)
                        End If
                    End Using
                End Using
                Return result
            End Function
        End Class
    End Namespace

    Form code where keyTextBox is a TextBox the user enters a key into

    Private Sub getKeyButton_Click(sender As Object, e As EventArgs) Handles getKeyButton.Click
        If Not String.IsNullOrWhiteSpace(keyTextBox.Text) Then
            Dim ops As New Operations
    
            Dim userKey = ops.GetApplicationKey(keyTextBox.Text)
    
            If Not String.IsNullOrWhiteSpace(userKey) Then
                MessageBox.Show("Entered a valid key")
            Else
                MessageBox.Show("Not a valid key")
            End If
        Else
            MessageBox.Show("Must enter a key")
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by RAT_SL Saturday, June 8, 2019 7:54 PM
    Friday, May 31, 2019 9:55 AM

All replies

  • Hi,

    Please provide your VB application code so we can assist you.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 31, 2019 5:11 AM
  • Hello,

    I dont have coe for this. i'm searching this for integrated with any application environment like that.  


    RohanD

    Friday, May 31, 2019 8:38 AM
  • Hello,

    The following was done using a SQL-Server database on a server with a different database name but by changing the connection string there is zero difference as the table is the same name and column name is the same. 

    If Kid is not a string field then rather than using CStr(keyValue) use CInt(keyValue).

    Class for checking the key entered

    Imports System.Data.SqlClient
    
    Namespace Classes
        Public Class Operations
            Private ConnectionString As String =
                        "Data Source=KARENS-PC;" &
                        "Initial Catalog=NorthWindAzureForInserts;" &
                        "Integrated Security=True"
            Public Function GetApplicationKey(ByVal pApplicationKey As String) As String
                Dim result as String = ""
                Dim selectStatement = "SELECT Kid FROM dbo.Kinfo WHERE Kid = @Kid"
                Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                    Using cmd As New SqlCommand With {.Connection = cn}
                        cmd.CommandText = selectStatement
                        cmd.Parameters.AddWithValue("@Kid", pApplicationKey)
                        cn.Open()
                        Dim keyValue = cmd.ExecuteScalar()
                        If keyValue Is Nothing Then
                            result = ""
                        Else
                            result = CStr(keyValue)
                        End If
                    End Using
                End Using
                Return result
            End Function
        End Class
    End Namespace

    Form code where keyTextBox is a TextBox the user enters a key into

    Private Sub getKeyButton_Click(sender As Object, e As EventArgs) Handles getKeyButton.Click
        If Not String.IsNullOrWhiteSpace(keyTextBox.Text) Then
            Dim ops As New Operations
    
            Dim userKey = ops.GetApplicationKey(keyTextBox.Text)
    
            If Not String.IsNullOrWhiteSpace(userKey) Then
                MessageBox.Show("Entered a valid key")
            Else
                MessageBox.Show("Not a valid key")
            End If
        Else
            MessageBox.Show("Must enter a key")
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by RAT_SL Saturday, June 8, 2019 7:54 PM
    Friday, May 31, 2019 9:55 AM