Click here to Skip to main content
11,496,146 members (436 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C#
da = New SqlDataAdapter("SELECT * FROM student_info WHERE no= " & TextBox12.Text & "", DBConn) 
    da.Fill(ds, "student_info") 
Catch ex As Exception
    Exit Sub
End Try
pls convert this code to c#
pls pls
Posted 24-Feb-12 3:43am
Edited 24-Feb-12 4:07am
I think your problem is not that you cannot convert to C#. There are plenty of websites and tools that can do this for you. Try Google and you'll find plenty of websites. I prefer Developer Fusion[^]. Actually a fellow CP'ian has written a blog post about this, especially because many people on CP ask for conversion tools. Read it here: .NET Code Conversion - Convert your code[^]

However, and I'm sorry to break this to you, but your code is flawed on many levels.
Take your query string, you simply build it and paste some user input in it. Well, suppose this user types something like ; --drop table student_info... Or worse! Even a simple and innocent input, like "D'Artagnan" will break your query. This is called SQL injection[^] and is very dangerous (and unfortunately very common).
So you need to parameterize your query. Luckily this is not hard. Consider the following code:
Dim cmd As New SqlCommand("SELECT * FROM student_info WHERE no = @Number", DBConn)
cmd.Parameters.AddWithValue("@Number", CInt(TextBox12.Text))
da = New SqlDataAdapter(cmd)
Notice how your code becomes better readable? Your query is now protected from SQL injection and "D'Artagnan" will not break it! What's more, SQL servers performance will increase when you use parameterized queries. It's a win win win situation!
However, this doesn't fix everything. Your TextBox12.Text is user input and thus must be validated. Perhaps the user didn't enter a valid Integer value.
So consider the following code, using Integer.TryParse[^]:
Dim userInput As Integer
If Integer.TryParse(TextBox12.Text, userInput) Then
   cmd.Parameters.AddWithValue("@Number", userInput)
   ' User did not type a valid numeric value.
   ' Possibly show them a MessageBox, whatever.
End If
So that brings us to the next point. Letting your users know if anything went wrong. You now catch an Exception and simply return like nothing happened. The user will wonder why they don't see their records... If you do NOT handle an Exception let it bubble up to the UI and at least show the user something has gone wrong there. Actually I have written an article on proper use of Try Catch blocks: Using Try... Catch..., Finally![^].
Another topic that article discusses is the Using block[^]. It's about cleaning up resources, which I don't see you do.
After you're done with your SqlCommand, your SqlConnection or your SqlDataAdapter you should properly dispose of them. The rule here is that when an Object Implements IDisposable[^] you should call Dispose[^] once you're done.

So now look at the completely revised code:
' I am assuming DBConn is a class field and might be used elsewhere.
' I assume ds is a class field.
   ' Put your SqlCommand in a Using block.
   Using cmd As New SqlCommand("SELECT * FROM student_info WHERE no = @Number")
      ' Validate the users input.
      Dim userInput As Integer
      If Integer.TryParse(TextBox12.Text, userInput) Then
         ' The users input was valid.
         cmd.Parameters.AddWithValue("@Number", userInput)
         ' put your SqlDataAdapter in a Using block.
         ' Not sure if a SqlDataAdapter automatically opens a connection, so...
         Using da As New SqlDataAdapter(cmd)
            da.Fill(ds, "student_info")
         End Using
         ' The users input was invalid.
         MessageBox.Show("Please enter a valid numeric value")
      End If
   End Using
Catch ex As Exception
   ' Perhaps log the Exception.
   MessageBox.Show(String.Format("An exception has occurred:{0}{1}", Environment.Newline, ex.Message))
   ' Closing a closed connection is not a problem.
   ' Don't dispose the DBConn yet if it is used elsewhere.
End Try
This code could be a lot more elegant. But this is a good start Smile | :)
I hope you learned a thing or two from my explanation. You can convert the code to C# yourself.
Good luck! Smile | :)
Mohammad A Rahman at 25-Feb-12 4:31am
Nice Explanation :)
Naerling at 25-Feb-12 4:34am
Thanks :)
thatraja at 25-Feb-12 4:59am
My Big 5! Don't know how did you get this much time for big explanation for this question(Not only this question, other questions too)? IMO you are one of the best writers in CP. I'm expecting more articles from you.

BTW Big Thanks for referred my Blogpost :)
Naerling at 25-Feb-12 6:10am
Thanks thatraja! I'm planning on writing some more articles, but it's not easy... Everytime I'm writing one I stumble upon some unforseen difficulties and never finish. But there's certainly more coming! :)
thatraja at 25-Feb-12 5:00am
BTW I'm going to update that blogpost within couple of days
Michel [mjbohn] at 25-Feb-12 5:33am
Great explanation!
Naerling at 25-Feb-12 6:11am
Thanks :)
Member 10492680 at 20-Jan-14 7:55am
Plz anyone tell me how to search record from datagrid view and fill text boxes having particular value...also check record exist or not when inserted...
da = new SqlDataAdapter("SELECT * FROM student_info WHERE no= " + TextBox12.Text + "", DBConn);
    da.Fill(ds, "student_info");

[edit]pre tag for C# added - PES[/edit]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 25 Feb 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100