Click here to Skip to main content
15,895,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir,
I have table name Attn in sql server with columns Emp_Id Emp_Name and Modify.
Modify have data like(Admin 30/08/2015). Now i want to search data from this table as
if Emp_Id=Idtxt.Text and Modify= TxtDate.text(here i want to pick only date not name Admin).
How can i do this...........
My original code below is:----------

VB
Sub LoadData_ID_Date()
        Dim cn As New SqlConnection(cnSettings2())
        cn.Open()
        da2 = New SqlDataAdapter("SELECT SqNo,Emp_Id,Emp_Name,Desig,Present,Modify,Verify_By from Attn where Emp_Id='" & TxtID.Text & "' and Modify = '" & Txt_Date.Text & "'", cn)
        dt2 = New DataTable
        da2.Fill(dt2)
        sBuilder2 = New SqlCommandBuilder(da2)
        dgv.Columns("SlNo").DataPropertyName = "SqNo"
        dgv.Columns("id").DataPropertyName = "Emp_Id"
        dgv.Columns("Nm").DataPropertyName = "Emp_Name"
        dgv.Columns("Desig").DataPropertyName = "Desig"
        dgv.Columns("Prsnt").DataPropertyName = "Present"
        dgv.Columns("Modi").DataPropertyName = "Modify"
        dgv.Columns("VerifyBy").DataPropertyName = "Verify_By"
        dgv.RowsDefaultCellStyle.BackColor = Color.White
        dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue
        dgv.AllowUserToAddRows = True
        dgv.Columns(0).Visible = False
        dgv.Columns(1).ReadOnly = True
        dgv.Columns(2).ReadOnly = True
        dgv.Columns(3).ReadOnly = True
        dgv.Columns(4).ReadOnly = False
        dgv.Columns(5).ReadOnly = False
        dgv.Columns(6).ReadOnly = True
        'list_items.Columns(0).DataPropertyName = "item_id"
        dgv.DataSource = dt2
        cn.Close()
        TxtID.Enabled = False
        Txt_Date.Enabled = False
        Txt_Vby.Focus()
    End Sub
Posted
Updated 30-Aug-15 17:30pm
v2

If I undestand your question correctly, you need to change the condition. If you search by name the include only
SQL
WHERE Emp_Id=@empid

in the WHERE clause and if you search by date then
SQL
WHERE Modify = @modifydate

The example code is different from what you have used since you should never concatenate values directly to the SQL statement. This leaves you for example open to SQL injections.

For more discussion, have a look at Properly executing database operations[^]
 
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