You must Sign In to post a response.
  • Category: Visual Studio

    How to compare the database value and string value in using csharp

    In database record as follows

    Coursename rctpno coursedate coursetimings

    AFF 242 24 Nov 14 13.15

    My code as follows in

    Dim AFF as string

    i want to compare the abvoe AFF value from database using if stamtent from the read

    for that how can i do in using C#.
  • #753936
    You can read the data from the data base using the below sample code.

    Con = new SqlConnection("Server=(local)\\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI");
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = ("Select * from table where name='"+ txtSearch.Text +"');
    cmd.Connection = con;
    try {
    sqlDataReader dr = cmd.Executereader();

    string AFF = dr["Id"].ToString() + " " + dr.["Address"].ToString();

    finally {

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #753940
    Hi Narasiman,

    Please find the sample code to compare the string value with db value and reading the matched row data.

    Public Sub CompareDBValue()
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim aaf As String = "AAF"
    con.ConnectionString = "Data Source=VDC01DDS-1072\SQLEXPRESS;Initial Catalog=UserDatabase;Integrated Security=SSPI;"
    cmd.CommandText = "select * from Course"
    cmd.CommandType = Data.CommandType.Text
    cmd.Connection = con
    Dim dA As New SqlDataAdapter
    Dim ds As New DataSet
    For Each tbRow In ds.Tables(0).Rows

    If String.Compare(aaf.ToLower(), tbRow("Coursename ").ToString().ToLower()) Then
    ' read the matched row data
    End If

    Catch ex As Exception

    End Try
    End Sub

    I hope this code will helps you.


  • #753950
    Hai Narsiman,
    You need to first fetch the records and then you can convert them to string and then you can compare with other string variable.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #753957

    I don't know VB, but i will tell you how to do this using C# you can convert that into VB.

    //First get database data and store it into one temporary table like datatable.

    DataTable dt=//database data;

    //assuming you have string data like below.

    string data="hello";

    now you want to compare string result with datatable result then use below sample code.

    //mis match

    Hope this will helpful to you..

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • Sign In to post your comments