• We just launched and are currently in beta. Join us as we build and grow the community.

Searching a Record From Database using C#

Zip

Digital Marketplace Creator
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
92
Likes
64
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
This tutorial is a continuation of our last topic called “How to Update and Delete Record in the Database using C#”. At this time we will be dealing with searching a Specific Record from the Database. To start with, we need to add another label, button and a textbox, then arranges it like as shown below.
a1_2.png


First we will set the visibility of the label and textbox for searching to false, so that when the application load it will not be visible to the user.

Second on the “Search Record” button, add the following code:
The code below will take effect when the search record button is clicked then it will show the label and a textbox that will allow the user to accept inputs for searching of a specific record.
  1. lblsearch.

    Show

    (

    )

    ;
  2. txtsearch.

    Show

    (

    )

    ;
  3. btnsearch.

    Enabled

    =

    false

    ;

At this time we will add code for textbox.To do this, double click the textbox and add the following code:
The code below will take effect when the text inside the textbox is changing because we are using the textchanged event of the textbox.
  1. dt =

    new

    DataTable(

    )

    ;
  2. string

    sql =

    "Select * from tbluseraccounts WHERE username LIKE '%"

    +

    txtsearch.

    Text

    +

    "%'"

    ;
  3. OleDbDataAdapter da =

    new

    OleDbDataAdapter(

    sql, conn)

    ;
  4. da.

    Fill

    (

    dt)

    ;
  5. dataGridView1.

    DataSource

    =

    dt;

After adding this code, you can test it by pressing the “F5” or the start button.
Take note!
You can modify the SQL Query using the Name or the Username of the user during searching of record.

And here’s all the code used in this application.
  1. using

    System

    ;
  2. using

    System.Collections.Generic

    ;
  3. using

    System.ComponentModel

    ;
  4. using

    System.Data

    ;
  5. using

    System.Drawing

    ;
  6. using

    System.Linq

    ;
  7. using

    System.Text

    ;
  8. using

    System.Windows.Forms

    ;
  9. using

    System.Data.OleDb

    ;

  10. namespace

    WindowsFormsApplication1
  11. {
  12. public

    partial

    class

    Form1 :

    Form
  13. {
  14. //declare new variable named dt as New Datatable
  15. DataTable dt =

    new

    DataTable(

    )

    ;
  16. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  17. static

    string

    connection =

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "

    +

    Application .

    StartupPath

    +

    "/usermgt.mdb"

    ;
  18. OleDbConnection conn =

    new

    OleDbConnection(

    connection)

    ;

  19. public

    Form1(

    )
  20. {

  21. InitializeComponent(

    )

    ;

    // calling the function
  22. }

  23. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  24. {

  25. }
  26. private

    void

    loadrecord(

    )
  27. {
  28. dt =

    new

    DataTable(

    )

    ;
  29. string

    sql =

    "Select * from tbluseraccounts"

    ;
  30. OleDbDataAdapter da =

    new

    OleDbDataAdapter(

    sql , conn)

    ;
  31. da.

    Fill

    (

    dt)

    ;
  32. dataGridView1.

    DataSource

    =

    dt;
  33. }

  34. private

    void

    button1_Click(

    object

    sender, EventArgs e)
  35. {
  36. loadrecord(

    )

    ;
  37. }

  38. private

    void

    btninsert_Click(

    object

    sender, EventArgs e)
  39. {

  40. OleDbCommand cmd =

    new

    OleDbCommand(

    )

    ;
  41. //set our SQL Insert INTO statement
  42. string

    sqlInsert =

    "INSERT INTO tbluseraccounts ( username, userusername, userpassword, usertype ) VALUES('"

    +

    txtname.

    Text

    +

    "','"

    +

    txtuser.

    Text

    +

    "','"

    +

    txtpass.

    Text

    +

    "','"

    +

    txttype.

    Text

    +

    "')"

    ;
  43. try
  44. {

  45. //open the connection
  46. conn.

    Open

    (

    )

    ;
  47. //set the connection
  48. cmd.

    Connection

    =

    conn;
  49. //get the SQL statement to be executed
  50. cmd.

    CommandText

    =

    sqlInsert;
  51. //execute the query
  52. cmd.

    ExecuteNonQuery

    (

    )

    ;
  53. //display a message
  54. MessageBox.

    Show

    (

    "New Record Added!...."

    )

    ;
  55. //close the connection
  56. conn.

    Close

    (

    )

    ;

  57. }
  58. catch

    (

    Exception ex)
  59. {
  60. //this will display some error message if something
  61. //went wrong to our code above during execution
  62. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  63. }
  64. //we call the loadrecord() function after adding a new record
  65. loadrecord(

    )

    ;


  66. }



  67. private

    void

    dataGridView1_CellContentClick(

    object

    sender, DataGridViewCellEventArgs e)
  68. {
  69. int

    i =

    e.

    RowIndex

    ;
  70. DataGridViewRow r =

    dataGridView1.

    Rows

    [

    i]

    ;

  71. int

    id =

    Convert.

    ToInt32

    (

    r.

    Cells

    [

    0

    ]

    .

    Value

    )

    ;
  72. txtuserID.

    Text

    =

    r.

    Cells

    [

    0

    ]

    .

    Value

    +

    ""

    ;

  73. }

  74. private

    void

    btnupdate_Click(

    object

    sender, EventArgs e)
  75. {
  76. OleDbCommand cmd =

    new

    OleDbCommand(

    )

    ;
  77. //set our SQL Insert INTO statement
  78. string

    sqlUpdate =

    "UPDATE tbluseraccounts set username ='"

    +

    txtname.

    Text

    +

    "' , userusername = '"

    +

    txtuser .

    Text

    +

    "', userpassword = '"

    +

    txtpass.

    Text

    +

    "', usertype= '"

    +

    txttype .

    Text

    +

    "' where userID = "

    +

    txtuserID .

    Text

    +

    ""

    ;
  79. try
  80. {

  81. //open the connection
  82. conn.

    Open

    (

    )

    ;
  83. //set the connection
  84. cmd.

    Connection

    =

    conn;
  85. //get the SQL statement to be executed
  86. cmd.

    CommandText

    =

    sqlUpdate;
  87. //execute the query
  88. cmd.

    ExecuteNonQuery

    (

    )

    ;
  89. //display a message
  90. MessageBox.

    Show

    (

    "Record Updated!...."

    )

    ;
  91. //close the connection
  92. conn.

    Close

    (

    )

    ;

  93. }
  94. catch

    (

    Exception ex)
  95. {
  96. //this will display some error message if something
  97. //went wrong to our code above during execution
  98. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  99. }
  100. //we call the loadrecord() function after adding a new record
  101. loadrecord(

    )

    ;
  102. }

  103. private

    void

    btndelteRecord_Click(

    object

    sender, EventArgs e)
  104. {
  105. OleDbCommand cmd =

    new

    OleDbCommand(

    )

    ;
  106. //set our SQL DELETE statement
  107. string

    sqlUpdate =

    "Delete * from tbluseraccounts where userID= "

    +

    txtuserID.

    Text

    +

    ""

    ;
  108. try
  109. {

  110. //open the connection
  111. conn.

    Open

    (

    )

    ;
  112. //set the connection
  113. cmd.

    Connection

    =

    conn;
  114. //get the SQL statement to be executed
  115. cmd.

    CommandText

    =

    sqlUpdate;
  116. //execute the query
  117. cmd.

    ExecuteNonQuery

    (

    )

    ;
  118. //display a message
  119. MessageBox.

    Show

    (

    "Record Deleted!...."

    )

    ;
  120. //close the connection
  121. conn.

    Close

    (

    )

    ;

  122. }
  123. catch

    (

    Exception ex)
  124. {
  125. //this will display some error message if something
  126. //went wrong to our code above during execution
  127. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  128. }
  129. //we call the loadrecord() function after adding a new record
  130. loadrecord(

    )

    ;
  131. }

  132. private

    void

    txtsearch_TextChanged(

    object

    sender, EventArgs e)
  133. {
  134. dt =

    new

    DataTable(

    )

    ;
  135. string

    sql =

    "Select * from tbluseraccounts WHERE username LIKE '%"

    +

    txtsearch.

    Text

    +

    "%'"

    ;
  136. OleDbDataAdapter da =

    new

    OleDbDataAdapter(

    sql, conn)

    ;
  137. da.

    Fill

    (

    dt)

    ;
  138. dataGridView1.

    DataSource

    =

    dt;

  139. }

  140. private

    void

    btnsearch_Click(

    object

    sender, EventArgs e)
  141. {
  142. lblsearch.

    Show

    (

    )

    ;
  143. txtsearch.

    Show

    (

    )

    ;
  144. btnsearch.

    Enabled

    =

    false

    ;
  145. }




  146. }
  147. }


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top