Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have a table in which the records are consumr_id,consumr_nm,consumr_add & consumr_cn
 
i want to view the record as per the consumr_cn (consumer contact number) given.. i am writing the code in lostfocus event of textbox... here the code is..
 
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\DIGISMART\details.mdb")
        cn.Open()
        
        cmd = New OleDbCommand("select * from product_details where consumr_cn='" & TextBox4.Text & "'", cn)
        On Error GoTo 0
        dr = cmd.ExecuteReader
 
        If dr.Read = True Then
            TextBox1.Text = dr(2)
            TextBox2.Text = dr(3)
            TextBox3.Text = dr(4)
 
        Else
        Me.ErrorProvider1.SetError(TextBox4, "Enter a Valied ID")
        TextBox4.Text = ""
        TextBox4.Focus()
        End If
                      
        dr.Close()
        cn.Close()
 

 
i am getting an error Data type mismatch in criteria expression on
dr = cmd.ExecuteReader
 
i already declared
Imports System.Data.OleDb
Imports System.Windows.Forms.Form
Public Class Form2
    Dim cn, cn1 As OleDbConnection
    Dim cmd, cmd1 As OleDbCommand
    Dim dt, dr As OleDbDataReader
what is wrong in my code.. pls help..
Posted 6-Sep-12 13:18pm
Comments
Sourav Sarkar-SS at 6-Sep-12 19:27pm
   
cmd = New OleDbCommand("select * from product_details where consumr_cn='" + TextBox4.Text + "'", cn)
try this
Member 8005145 at 7-Sep-12 3:24am
   
thanks. it works..
Wes Aday at 6-Sep-12 20:49pm
   
Try cmd = New OleDbCommand("select * from product_details where consumr_cn=" & TextBox4.Text, cn)
Member 8005145 at 7-Sep-12 3:24am
   
it also works
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try:
 
TextBox1.Text = dr(2).ToString()
TextBox2.Text = dr(3).ToString()
TextBox3.Text = dr(4).ToString()
  Permalink  
Comments
Member 8005145 at 7-Sep-12 3:25am
   
Thanks sir... :)
Kuthuparakkal at 7-Sep-12 3:33am
   
you're most welcome my friend!
Member 8005145 at 7-Sep-12 4:38am
   
But sir whenever i am adding string value in consumer contact number field there should be a message or error to show that the field is incorrect,it must be a numeric value.... i wrote the code as
<pre lang="vb">If TextBox4.Text = "" Or TextBox4.Text <> Val(TextBox4.Text) Then

If TextBox4.Text = "" Then
Me.ErrorProvider1.SetError(TextBox4, "Please Enter Contact number")
TextBox4.Focus()
ElseIf TextBox4.Text <> Val(TextBox4.Text) Then
Me.ErrorProvider1.SetError(TextBox4, "Please Enter a valid Contact number ")
TextBox4.Focus()
end if
End if</pre>
 
it doesnt work... i have created access table in where the consumr_cn data type is number(Long Integer)
 
the error is in
 
<pre lang="vb">TextBox4.Text = "" Or TextBox4.Text <> Val(TextBox4.Text)</pre>
suppose the entered data in consumer contact number is abcd.. the error is "Conversion from string "abcd" to type 'Double' is not valid."
i just wanted to check whether the entered value is numeric or not.. is the code wrong for my criteria?
Kuthuparakkal at 7-Sep-12 4:47am
   
use different validation strategy:
Dim number As Integer
Dim result As Boolean = Int32.TryParse(TextBox4.Text, number)
If not result Then
Me.ErrorProvider1.SetError(TextBox4, "Please Enter a valid Contact number ")
End if
Member 8005145 at 7-Sep-12 5:01am
   
yahooooo!!!! it works..... thanks again.. thank u very very much for helping the new learners like me.... thank u and thanks to the code project.. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Before executing the select command you must have to convert the value of textbox4 into the data type as per data type of your column data type.
When you create your database you specify the data type of the field - consumr_cn
convert your textbox4.value into that data type then execute select command.
for conversion -
1. Into Integer
 
CInt(textbox4.text)
 
2. Into double
Convert.toDouble(textbox4.text)
 
3. Into Int16,32,64
 
Convert.toIn16(textbox4.text)
Convert.toIn32(textbox4.text)
Convert.toIn64(textbox4.text)
 
then -
 
cmd = New OleDbCommand("select * from product_details where consumr_cn=" & TextBox4.Text & "", cn)
  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 459
1 OriginalGriff 441
2 sanket saxena 349
3 Tadit Dash 310
4 Abhinav S 235
0 Sergey Alexandrovich Kryukov 12,109
1 OriginalGriff 7,326
2 Peter Leow 5,013
3 Abhinav S 4,013
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid