exceter Posted July 10, 2004 Posted July 10, 2004 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 ??? Quote
Administrators PlausiblyDamp Posted July 10, 2004 Administrators Posted July 10, 2004 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() Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
exceter Posted July 10, 2004 Author Posted July 10, 2004 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. Quote
Administrators PlausiblyDamp Posted July 10, 2004 Administrators Posted July 10, 2004 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
exceter Posted July 10, 2004 Author Posted July 10, 2004 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) Quote
Administrators PlausiblyDamp Posted July 10, 2004 Administrators Posted July 10, 2004 In that case what does the variable searchPart contain when the error occurs? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
exceter Posted July 10, 2004 Author Posted July 10, 2004 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. Quote
exceter Posted July 10, 2004 Author Posted July 10, 2004 Error message is as following: ex.Message = Input string was not in a correct format. ex.Source = mscorlib Quote
Administrators PlausiblyDamp Posted July 12, 2004 Administrators Posted July 12, 2004 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? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted July 12, 2004 *Experts* Posted July 12, 2004 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 Quote "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
exceter Posted July 13, 2004 Author Posted July 13, 2004 Thank you, Seems I understand what I was doing wrong. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.