Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Marks me that I can return a null value at the time of executing it, but the video I was watching for the realization of the project if I let you return that value
VB.NET
Public Function consultarCliente(numID As String) As DataTable()
       Try
           conectar()
           cmd = New SqlCommand("select ID, Nombres, TELEFONO FROM CLIENTES WHERE NUM_ID='" & numID & "'")

           cmd.Connection = con
           If cmd.ExecuteNonQuery Then

               Dim dt As New DataTable

               Dim adp As New SqlDataAdapter(cmd)

               adp.Fill(dt)

               Return dt
           Else
               Return Nothing
           End If
       Catch ex As Exception
           MsgBox(ex.Message)
       End Try

   End Function

see the video developer of project, investigation in web page error

What I have tried:

see the video developer of proyect, investigation in web page error
Posted
Updated 19-Jul-23 8:00am
v2
Comments
Member 15627495 19-Jul-23 12:46pm    
adp is fill by dt.
but dt is empty.

I have no idea what you mean by "Marks me that I can return a null value at the time of executing it, but the video I was watching for the realization of the project if I let you return that value" or "see the video developer of project, investigation in web page error" - as English sentences they have no meaning. Please use Google Translate if you aren't confident in English.

But ... ExecuteNonQuery returns an integer: you are SELECTing a table of data which may return numerous rows. Quite what you expect this to evaluate to as a boolean value I'm not sure - but I suspect your app will fail with an exception at that point.

Dump the text completely, and just retrieve the rows into your DataTable - it'll be quicker and clearer.

But ... don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 
Your question makes no sense at all. I have no idea what the complaint or even the error message is. If you get an error message, copy and paste the exact message into your question.

So little code and so many problems.

The first line of what you posted is wrong. There should NOT be parentheses on the DataTable at the end of the line:
VB.NET
Public Function consultarCliente(numID As String) As DataTable


You appear to be storing record ID numbers as some arbitrary string. That should NEVER be the case. Record ID values should NEVER be actual business data.

You're querying the database TWICE for no reason. Each query is returning the exact same data, once to check to see if the query returns anything and again to fill in a DataTable object. You only need to run the query ONCE, to fill in the DataTable.

You're subjecting your code to SQL Injection attacks by using string concatenation to build the SQL statement. Use parameterized queries instead.

Your function doesn't return an object on all code paths. If there's an exception, you display a message and then don't return anything. That's probably what the error message is about.

Your function should not be displaying an error message at all. Its job is to get data from the database and return it, nothing more. Any errors, and displaying of messages, should be handled by the code that calls this function.
 
Share this answer
 
Try this

Public Function consultarCliente(numID As String) As DataTable
Try
conectar()
cmd = New SqlCommand("select ID, Nombres, TELEFONO FROM CLIENTES WHERE NUM_ID='" & numID & "'")
cmd.Connection = con

Dim dt As New DataTable
Dim adp As New SqlDataAdapter(cmd)
adp.Fill(dt)

Return If(dt.Rows.Count > 0, dt, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function
This version handles null results more succinctly and should help you with those error marks.
 
Share this answer
 
Comments
Richard Deeming 20-Jul-23 5:06am    
You have copied the SQL Injection[^] vulnerability from the question. NEVER use string concatenation/interpolation to build a SQL query. ALWAYS use a parameterized query.

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