Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..

VB
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
VB
dr = cmd.ExecuteReader


i already declared
VB
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
Comments
[no name] 6-Sep-12 19:27pm    
cmd = New OleDbCommand("select * from product_details where consumr_cn='" + TextBox4.Text + "'", cn)
try this
Member 8005145 7-Sep-12 3:24am    
thanks. it works..
[no name] 6-Sep-12 20:49pm    
Try cmd = New OleDbCommand("select * from product_details where consumr_cn=" & TextBox4.Text, cn)
Member 8005145 7-Sep-12 3:24am    
it also works

Try:

VB
TextBox1.Text = dr(2).ToString()
TextBox2.Text = dr(3).ToString()
TextBox3.Text = dr(4).ToString()
 
Share this answer
 
Comments
Member 8005145 7-Sep-12 3:25am    
Thanks sir... :)
Kuthuparakkal 7-Sep-12 3:33am    
you're most welcome my friend!
Member 8005145 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 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 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.. :)
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

VB
CInt(textbox4.text)


2. Into double
C#
Convert.toDouble(textbox4.text)


3. Into Int16,32,64

C#
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)
 
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