Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to search a record that in SQL Database table which the searching algorithm is depend on a textbox value. It means that the String

C#
sEmpID = txtEmpID.text;


If I enter a value to the txtEmpID textbox then the result should gain using that value. It should retrieve only one record. To do that I can use a dataset. But in dataset the primary key is set on the datasets' side. But when I want to add a primary key from the database it would create a duplicate primary key. That will generate an error.

Is there any other way to do that using a Dataset ?

Updated Question

DbConn Class

C#
 Imports System.Data.SqlClient

Imports System.Data.DataTable

Public Class DbConn

    'Declare instant Variables
    Public DbConnct As New SqlConnection
    Public DbComm As New SqlCommand
    Public DbAdapt As New SqlDataAdapter

    Public dsItemPurchase As New DataSet
   
    Public dtItemPurchase As New DataTable
    
    Public objICode As New Object

     Public Sub Conn()
        'Database Connection
     
        DbConnct.ConnectionString = "Data Source=Chiranthaka_HP\SQLSERVER2008R2;Initial Catalog=SenMax;Integrated Security=True"
        DbConnct.Open()
    End Sub
    Public Sub AdaptIPurchse()

        DbAdapt = New SqlDataAdapter("Select * From ItemPurchase", DbConnct)
       
        DbAdapt.Fill(dsItemPurchase, "ItemPurchase")
        DbAdapt.Fill(dtItemPurchase)

        'Set PrimaryKey
        dsItemPurchase.Tables("ItemPurchase").PrimaryKey = New DataColumn() {dsItemPurchase.Tables("ItemPurchase").Columns("ItemCode")}
    End Sub


The Form

C#
 Imports System.Data.SqlClient
Public Class UStockDetails
   Inherits System.Web.UI.Page
   Private DataConn As New DbConn

   Protected Sub btnFind_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFind.Click
       'Check the ItemCode
       If Not (txtICode.Text = "") Then

           'Declare the Object Variable
           ' objICode = txtICode.Text
           DataConn.objICode = txtICode.Text


           'Declare the Find DataRow
           Dim FindRow As Data.DataRow = DataConn.dsStockDetails.Tables("StockDetails").Rows.Find(objICode)
           ' Dim FindRow As Data.DataRow = DataConn.dtStockDetails.Rows.Find(DataConn.objICode)
           If Not (FindRow Is Nothing) Then

               'Assign the Record to the Texboxes
               txtIType.Text = FindRow("ItemType")
               txtTitle.Text = FindRow("Title")
               txtNoDVD.Text = FindRow("NoDVD")
               txtQTY.Text = FindRow("QTY")
               txtPrice.Text = FindRow("PricePUnit")
               txtBDate.Text = FindRow("BuyDate")
               txtSName.Text = FindRow("SName")
               txtSID.Text = FindRow("SID")

               'Set th Focus
               txtICode.Focus()

               'Display the Message
               lblMSG.Text = "Record Found"
           Else
               'Display the Message
               lblMSG.ForeColor = Drawing.Color.Red
               lblMSG.Text = "Record Not Found"
           End If

       End If
   End Sub

End Class


Now you can see I have used a dataset but in dataset I have to create a primary key! But it do not allow to create primary key for the database table from the database. If created it create an error. To prevent the solution is said to be the DataTable(). Then my question is how to filter 01 record only. That record is filtered according to the text in the sEmpNo.

That is what I needed.
Posted
Updated 29-May-13 19:37pm
v2
Comments
msaroka 29-May-13 23:01pm    
Are you looking to set the primary key for new rows? or just get what the next key will be when adding a new record?
Chiranthaka Sampath 30-May-13 1:37am    
See the updated question !
Sunasara Imdadhusen 30-May-13 0:48am    
Not clear!!
Chiranthaka Sampath 30-May-13 1:37am    
See the updated question !

1 solution

Instead of ....
VB
Dim FindRow As Data.DataRow = DataConn.dsStockDetails.Tables("StockDetails").Rows.Find(objICode)

use ...
VB
Dim rows = DataConn.dsStockDetails.Tables("StockDetails").Select(String.Format("sEmpNo = {0}",txtICode.Text))
if(rows.GetLength(0) != 0) Then FindRow = rows[0] 

Excuse me if there are VB errors I am used to C#
 
Share this answer
 
Comments
Chiranthaka Sampath 31-May-13 1:29am    
I think that will me solve the problem. Thanx anyway !!!!!!!!

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