0

I previously had a code in VB.NET that searched for a value in the DB and provided me with a result. This was the code:

    Class UserData
    Dim theResults = New List(Of UserData)
    Property Name As String
    Property LastName As String

End Class

Function GetData(ByVal clientNo As Integer) As List(Of UserData)


    Dim theResults = New List(Of UserData)
    Dim connStr = "serverstring"


    Using conn = New SqlConnection(connStr)

        Dim sql = "SELECT [Name], [PaternalLastName], [MaternalLastName] FROM [SocioInfo] Where ([SocioNum] = @SocioNum)"
        Dim sqlCmd = New SqlCommand(sql, conn)
        sqlCmd.Parameters.AddWithValue("@SocioNum", CStr(txtInput.Text))



        conn.Open()

        Dim rdr = sqlCmd.ExecuteReader

        While rdr.Read
            theResults.Add(New UserData With {
                           .Name = rdr.GetString(0),
                           .LastName = rdr.GetString(1),
                           .Maternal = rdr.GetString(2)
})

        End While

        conn.Close()

    End Using

    Return theResults

End Function

 Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click


    Dim clientNo As Integer = 0
    Dim myResults = GetData(clientNo)

    If Integer.TryParse(txtInput.Text, clientNo) Then
        If myResults.Count = 1 Then
          Execute Code
        Else
            MessageBox.Show("NAH")

        End If
    Else
        MessageBox.Show("NAH")
    End If


End Sub

Now I tried to convert this code to C# but failed miserably. I used some converters I found online and it threw exceptions so I tried doing it by hand but I don't know the codes or correct way to do it since I am still learning my ropes in C#. If anyone could help it'd be greatly appreciated :)

C# Code:

{
 theResults = new List<UserData>();
public string Nombre { get; set; }
public string Apellido { get; set; }
public string ApellidoMaterno { get; set; }


}


    public List<UserData> GetData(int clientNo)
    {


        dynamic theResults = new List<UserData>();
        dynamic connStr = "Data Source=lsf-corapbk-01;Initial Catalog=Custmast_CO;Integrated Security=Yes";


        using (conn == new SqlConnection(connStr))
        {

            dynamic sql = "SELECT [Name], [PaternalLastName], [MaternalLastName] FROM [SocioInfo] Where ([SocioNum] = @SocioNum)";
            dynamic sqlCmd = new SqlCommand(sql, conn);
            sqlCmd.Parameters.AddWithValue("@SocioNum", Convert.ToString(txtInput.Text));



            conn.Open();

            dynamic rdr = sqlCmd.ExecuteReader;

            while (rdr.Read)
            {
                theResults.Add(new UserData
                {
                    Nombre = rdr.GetString(0),
                    Apellido = rdr.GetString(1),
                    ApellidoMaterno = rdr.GetString(2)
                });

            }

            conn.Close();

        }

        return theResults;

    }

Most of the exceptions are in the UserData area.

3
  • What C# code do you have, and what parts of it are you having trouble with? Commented Aug 9, 2013 at 15:53
  • Online Converters are not going to do as good a job as hand-written conversion. Also, there are idioms between VB/C#... If you convert yourself, you'll learn C# and those idioms. Commented Aug 9, 2013 at 15:53
  • 1
    Post the C# code and what exception(s) you're getting instead of the VB code. Commented Aug 9, 2013 at 15:57

1 Answer 1

1
   public List<UserData> GetData(int clientNo)
    {
        var theResults = new List<UserData>();
        var connStr = "serverstring";

        using (var cnn = new SqlConnection(connStr))
        {
            cnn.Open();
            using (var cmd = new SqlCommand("SELECT [Name], [PaternalLastName], [MaternalLastName] FROM [SocioInfo] Where ([SocioNum] = @SocioNum)",cnn))
            {
                cmd.Parameters.AddWithValue("@SocioNum", txtInput.Text);
                using (var rdr = cmd.ExecuteReader())
                {
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            theResults.Add(new UserData
                                {
                                    Name = rdr.GetString(0),
                                    LastName = rdr.GetString(1),
                                    Maternal = rdr.GetString(2)
                                });
                        }
                    }
                }
            }
        }
        return theResults;
    }

 public class UserData
        {
            public string Name { get; set; }
            public string LastName { get; set; }
            public string Maternal { get; set; }
        }

The button click would be...

private void btnSearch_Click(object sender, EventArgs e)
{
    int clientNo = 0;
    var myResults = GetData(clientNo);
    if (int.TryParse(txtInput.Text, clientNo))
    {
        if ((myResults.Count == 1))
        {
            //Execute Code
        }
        else
        {
            MessageBox.Show("NAH");
        }
    }
    else
    {
        MessageBox.Show("NAH");
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

How would I go about coding the UserData list? It's where most exceptions are being thrown.
Alright I gave your code a go but its giving me the following error: The type or namespace name 'UserData' could not be found (are you missing a using directive or an assembly reference?) Thanks for your time :)
@LordRelix, I will format it now - but, that should be it for the reader. I take it you're ok with the btnSearch part?
Yeah I had the search button nailed down, thanks for that still. Will check the code and see if it works as it should, hope it does. Thanks a million Christian.
@LordRelix, post here if it worked or if anything needs changing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.