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

    How to compare the database value and string value in vb.net using csharp

    In database record as follows

    Coursename rctpno coursedate coursetimings

    AFF 242 24 Nov 14 13.15


    My code as follows in vb.net

    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 vb.net 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 {
    con.Open();
    sqlDataReader dr = cmd.Executereader();

    if(dr.hasRows)
    {
    while(dr.Read())
    {
    string AFF = dr["Id"].ToString() + " " + dr.["Address"].ToString();
    }
    }

    }
    finally {
    con.Close();
    }

    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()
    Try
    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
    dA.Fill(ds)
    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

    Next
    Catch ex As Exception

    End Try
    End Sub

    I hope this code will helps you.

    Thanks,
    Murali.MD

  • #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.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #753957
    Hi,

    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.

    if(data==dt.Rows[rowindex]["col"].ToString())
    {
    //matches
    }
    else
    {
    //mis match
    }


    Hope this will helpful to you..

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

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments