Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
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
Updated 24-Feb-12 3:07am
v2

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:
VB
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[^]:
VB
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:
VB
' 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 :)
I hope you learned a thing or two from my explanation. You can convert the code to C# yourself.
Good luck! :)
 
Share this answer
 
v2
Comments
Mohammad A Rahman 25-Feb-12 4:31am    
Nice Explanation :)
Sander Rossel 25-Feb-12 4:34am    
Thanks :)
thatraja 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 :)
Sander Rossel 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 25-Feb-12 5:00am    
BTW I'm going to update that blogpost within couple of days
 
Share this answer
 
C#
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]
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900