Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
ds.Clear() 
da = New SqlDataAdapter("SELECT * FROM student_info WHERE no= " & TextBox12.Text & "", DBConn) 
Try 
    da.Fill(ds, "student_info") 
    Bind()
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
Estys12.5K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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)
Else
   ' 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.
Try
   ds.Clear()
   ' 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...
         DBConn.Open()
         Using da As New SqlDataAdapter(cmd)
            da.Fill(ds, "student_info")
            Bind()
         End Using
      Else
         ' 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))
Finally
   ' Closing a closed connection is not a problem.
   DBConn.Close
   ' 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 | :)
  Permalink  
v2
Comments
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...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

ds.Clear();
da = new SqlDataAdapter("SELECT * FROM student_info WHERE no= " + TextBox12.Text + "", DBConn);
try
{
    da.Fill(ds, "student_info");
    Bind();
 
}
catch
{
   return;
}
 
[edit]pre tag for C# added - PES[/edit]
  Permalink  
v2

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 420
1 OriginalGriff 230
2 DamithSL 135
3 Dave Kreskowiak 110
4 Suvendu Shekhar Giri 110
0 OriginalGriff 7,740
1 DamithSL 5,644
2 Sergey Alexandrovich Kryukov 5,404
3 Maciej Los 5,011
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 25 Feb 2012
Copyright © CodeProject, 1999-2014
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