Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I have created a small vb.net program.

I need to retrive data from sql by vb.net and modify it but when I run it, it returns with the first record at sql table.

That means the query is not executed.
Can you please check this code and give me a solution


VB
Imports System.Data.SqlClient
Imports System.Data
Imports System.IO

Public Class main

    Private sqlconn As New SqlConnection("Data Source=MODY-PC;Initial Catalog=Sosa;User ID=sa;Password=password123")
    Private da As New SqlDataAdapter("SELECT * from EM_TBL", sqlconn)
    Private coomd As New SqlCommand("SELECT * from EM_TBL where Where EM_NAME =@ddd", sqlconn)
    Private ds As New DataSet
    Private cmb As New SqlCommandBuilder(da)
    Private cmd As New SqlCommand


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
        Login_Form.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ''search button


        Try
            coomd.Parameters.AddWithValue("@ddd", TextBox1.Text)
            da.Fill(ds, "EM_TBL")
            TextBox3.DataBindings.Add("text", ds, "EM_TBL.EM_NAME")
            TextBox2.DataBindings.Add("text", ds, "EM_TBL.EM_MID_NA")
            TextBox4.DataBindings.Add("text", ds, "EM_TBL.EM_LAST")
            TextBox5.DataBindings.Add("text", ds, "EM_TBL.JOP")
            TextBox6.DataBindings.Add("text", ds, "EM_TBL.ID_NO")
            TextBox7.DataBindings.Add("text", ds, "EM_TBL.PASS_NO")
            TextBox8.DataBindings.Add("text", ds, "EM_TBL.PASS_DATE")
            TextBox9.DataBindings.Add("text", ds, "EM_TBL.RES_EXP_DATE")
            TextBox10.DataBindings.Add("text", ds, "EM_TBL.NATIONALITY")
            TextBox11.DataBindings.Add("text", ds, "EM_TBL.MOB1")
            TextBox12.DataBindings.Add("text", ds, "EM_TBL.MOB2")
            TextBox13.DataBindings.Add("text", ds, "EM_TBL.HOME_NO")
            TextBox14.DataBindings.Add("text", ds, "EM_TBL.ADDRESS")
            TextBox15.DataBindings.Add("text", ds, "EM_TBL.COMMENT")
            sqlconn.Close()
        Catch ex As Exception

        End Try
    End Sub

[edit]Converted from "inline code" to "code block" = OriginalGriff[/edit]
Posted
Updated 11-Jan-11 21:38pm
v3
Comments
Dalek Dave 12-Jan-11 3:38am    
Edited for Grammar, Syntax and Readability.

Not clear with what you want and the coding. As far as I understood this is a simple retrieve function, which on googling you get so many examples, Copy and paste the code, change the sql statement and do other changes accordingly.

Here is the first link[^]which I got on the googling :)
 
Share this answer
 
v2
Comments
Dalek Dave 12-Jan-11 3:38am    
Fair Enough.
Remove one of the "WHERE" words from this line:
VB
Private coomd As New SqlCommand("SELECT * from EM_TBL where Where EM_NAME =@ddd", sqlconn)

When it lloks like this:
Private coomd As New SqlCommand("SELECT * FROM EM_TBL WHERE EM_NAME =@ddd", sqlconn)
It will work better...
 
Share this answer
 
Comments
Prerak Patel 12-Jan-11 3:27am    
+5: Good call.
Dalek Dave 12-Jan-11 3:38am    
Excellent answer.
Baji Jabbar 12-Jan-11 3:48am    
good catch +5
i remove where but i dont know why return by firt record in db that mean it return with the value from data adepter (select * from EM_TBL)
i need the value from search command not from the sql adapter
please fix to me if i am wrong
i appreciate your answer
 
Share this answer
 
Remove one where in your code
Private comd As New SqlCommand("SELECT * FROM patient WHERE where pname=@ddd", sqlconn)

Like This Code:
Private comd As New SqlCommand("SELECT * FROM patient WHERE pname=@ddd", sqlconn)

And If connection Does Not Exit Then try this;
Private sqlconn As New SqlConnection("Data Source=MODY-PC;Initial Catalog=Sosa;AttachDbFilename=|DataDirectory|\reciept.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
 
Share this answer
 

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