Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I try the same (SELECT * FROM documents WHERE name_doc='Fax')in Query Builder, there I have no error,

but in WebApplication i have error:

Input string was not in a correct format.

 

Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click          
   If IsValid Then
             Try
                 SqlConnection1.Open()
                 Dim NameS As String = txtName.Text
                 Dim DataAdapter1 As New SqlClient.SqlDataAdapter("Select * From Documents Where Name_doc ='" & CType(NameS, String) & "'", SqlConnection1)
                 Dim Command As New SqlClient.SqlCommandBuilder(DataAdapter1)
                 Dim DataSet99 As New DataSet
                 DataAdapter1.FillSchema(DataSet99, SchemaType.Source, "Documents")
                 DataSet99.Clear()
                 DataAdapter1.Fill(DataSet99, "Documents")
                   Dim objRow As DataRow
                 objRow = DataSet99.Tables("Documents").Rows.Find(searchPart)
                  txtInNum.Text = objRow.Item("cod_int")
                  txtExNum.Text = objRow.Item("cod_ext")
                  SqlConnection1.Close()
             Catch ex As Exception
                 ErrorMsg.Text = ex.Message
                 ErrorMsg.Visible = True
             End Try
         End If
End Sub  

 

What I am doing wrong ???

  • Administrators
Posted

In the line

Dim DataAdapter1 As New SqlClient.SqlDataAdapter("Select * From Documents Where Name_doc ='" & CType(NameS, String) & "'", SqlConnection1)

 

what is the value of NameS? Also you may want to replace

CType(NameS, String)

with

NameS.ToString()

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

1.

In the line what is the value of NameS?

Dim NameS As String = txtName.Text

2.

Also you may want to replace

CType(NameS, String)

with

NameS.ToString()

That changes Nothing.

Posted
But what was the value of NameS? If you step through the debugger and put a watch on NameS what value does it contain?

Also which line is causing the error to occur?

 

the value of NameS is what I want to search the Database, for example, "FAX" (the database contains the titles of the articles).

 

the Error occurs at line:

objRow = DataSet99.Tables("Documents").Rows.Find(NameS)

Posted
In that case what does the variable searchPart contain when the error occurs?

 

NameS and searchPart are the same.

 

the value of NameS is as I said "FAX", the title of Article which I want to search for.

  • Administrators
Posted

If you step through the code in the debugger does searchPart really contain the word "FAX" as I can't see you assigning anything to it in the code snippet you posted above.

 

Out of interest does the file have Option Explicit On and Option Strict On at the top?

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • *Experts*
Posted

Four things:

1. Since NameS is declared as string you don't need a CType or a ToString on it. Not important, but thought I'd mention it.

 

2. A better thing to do than CType would be to Replace any single quotes with double single quotes. Without that, a malicious user could send commands to your database. Something like this would help:

Dim NameS As String = txtName.Text.Trim().Replace("'", "''")

 

3. If searchPart is just "Fax" then you can't use that in the Find method. The find method wants a pseudo-WHERE clause. A filter would look something like "Name_doc = 'fax'". Here's a code snippet:

searchPart = "Name_doc ='" & NameS & "'"
objRow = DataSet99.Tables("Documents").Rows.Find(searchPart)

 

4. If NameS and searchPart are meant to filter on the same column/value then I don't know WHY you'd do the Find. It's not going to filter any more than the SQL call.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...