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

SQL server 2008 Save,Edit,Delete and Search from Datagridview Vb.net 2008

elbrent12341

Cyber Intelligence Analyst
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
95
Likes
169
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 1000 XP
  1. Imports

    System.

    Data
  2. Imports

    System.

    Data

    .

    SqlClient
  3. Public

    Class

    frmRID
  4. Dim

    sCommand As

    SqlCommand
  5. Dim

    sAdapter As

    SqlDataAdapter
  6. Dim

    sBuilder As

    SqlCommandBuilder
  7. Dim

    sDs As

    DataSet
  8. Dim

    sTable As

    DataTable

  9. Private

    Sub

    cnew_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    cnew.

    Click
  10. Call

    main(

    )
  11. Con.

    Open

    (

    )
  12. If

    cnew.

    Text

    =

    "New"

    Then

  13. cnew.

    Text

    =

    "Save"
  14. cedit.

    Text

    =

    "Cancel"
  15. clear(

    )

  16. unlock(

    )
  17. cdel.

    Enabled

    =

    False
  18. csearch.

    Enabled

    =

    False
  19. 'txt1.Enabled = False

  20. Dim

    sql As

    String

    =

    "SELECT * FROM DOrecieve_inventory"
  21. 'Dim connection As New SqlConnection '(connectionString)
  22. 'connection.Open()
  23. sCommand =

    New

    SqlCommand(

    sql, Con)
  24. sAdapter =

    New

    SqlDataAdapter(

    sCommand)
  25. sBuilder =

    New

    SqlCommandBuilder(

    sAdapter)
  26. sDs =

    New

    DataSet(

    )
  27. sAdapter.

    Fill

    (

    sDs, "DOrecieve_inventory"

    )
  28. sTable =

    sDs.

    Tables

    (

    "DOrecieve_inventory"

    )
  29. Con.

    Close

    (

    )
  30. grid1.

    DataSource

    =

    sDs.

    Tables

    (

    "DOrecieve_inventory"

    )
  31. 'grid1.ReadOnly = True
  32. grid1.

    SelectionMode

    =

    DataGridViewSelectionMode.

    FullRowSelect

  33. Else
  34. cnew.

    Text

    =

    "Save"
  35. cnew.

    Text

    =

    "New"
  36. cedit.

    Text

    =

    "Update"
  37. save(

    )
  38. clear(

    )
  39. lock(

    )
  40. cdel.

    Enabled

    =

    True
  41. csearch.

    Enabled

    =

    True
  42. End

    If
  43. End

    Sub
  44. Private

    Sub

    clear(

    )
  45. 'grid1.Rows.Clear()
  46. End

    Sub
  47. Private

    Sub

    unlock(

    )
  48. 'grid1.Enabled = True
  49. End

    Sub
  50. Private

    Sub

    lock(

    )
  51. 'grid1.Enabled = False
  52. End

    Sub
  53. Private

    Sub

    save(

    )
  54. Call

    main(

    )
  55. Con.

    Open

    (

    )

  56. Dim

    nonqueryCommand As

    SqlCommand =

    Con.

    CreateCommand

    (

    )

  57. Try

  58. nonqueryCommand.

    CommandText

    =

    _
  59. "INSERT INTO DOrecieve_inventory (type,lot,reference,location,meas,units,pounds) VALUES (@type,@lot,@reference,@location,@meas,@units,@pounds)"

  60. ' Add Parameters to Command Parameters collection
  61. nonqueryCommand.

    Parameters

    .

    Add

    (

    "@type"

    , SqlDbType.

    VarChar

    , 50

    )
  62. nonqueryCommand.

    Parameters

    .

    Add

    (

    "@lot"

    , SqlDbType.

    VarChar

    , 50

    )
  63. nonqueryCommand.

    Parameters

    .

    Add

    (

    "@reference"

    , SqlDbType.

    VarChar

    , 50

    )
  64. nonqueryCommand.

    Parameters

    .

    Add

    (

    "@location"

    , SqlDbType.

    VarChar

    , 50

    )
  65. nonqueryCommand.

    Parameters

    .

    Add

    (

    "@meas"

    , SqlDbType.

    VarChar

    , 50

    )
  66. nonqueryCommand.

    Parameters

    .

    Add

    (

    "@units"

    , SqlDbType.

    VarChar

    , 50

    )
  67. nonqueryCommand.

    Parameters

    .

    Add

    (

    "@pounds"

    , SqlDbType.

    VarChar

    , 50

    )


  68. ' Prepare command for repeated execution
  69. nonqueryCommand.

    Prepare

    (

    )

  70. ' Data to be inserted
  71. For

    Each

    row As

    DataGridViewRow In

    grid1.

    Rows
  72. If

    Not

    row.

    IsNewRow

    Then
  73. nonqueryCommand.

    Parameters

    (

    "@type"

    )

    .

    Value

    =

    row.

    Cells

    (

    0

    )

    .

    Value

    .

    ToString
  74. nonqueryCommand.

    Parameters

    (

    "@lot"

    )

    .

    Value

    =

    row.

    Cells

    (

    1

    )

    .

    Value

    .

    ToString
  75. nonqueryCommand.

    Parameters

    (

    "@reference"

    )

    .

    Value

    =

    row.

    Cells

    (

    2

    )

    .

    Value

    .

    ToString
  76. nonqueryCommand.

    Parameters

    (

    "@location"

    )

    .

    Value

    =

    row.

    Cells

    (

    3

    )

    .

    Value

    .

    ToString
  77. nonqueryCommand.

    Parameters

    (

    "@meas"

    )

    .

    Value

    =

    row.

    Cells

    (

    4

    )

    .

    Value

    .

    ToString
  78. nonqueryCommand.

    Parameters

    (

    "@units"

    )

    .

    Value

    =

    row.

    Cells

    (

    5

    )

    .

    Value

    .

    ToString
  79. nonqueryCommand.

    Parameters

    (

    "@pounds"

    )

    .

    Value

    =

    row.

    Cells

    (

    6

    )

    .

    Value

    .

    ToString
  80. 'MsgBox("record save")

  81. End

    If
  82. Next

  83. nonqueryCommand.

    ExecuteNonQuery

    (

    )

  84. Catch

    ex As

    SqlException
  85. ' Display error
  86. Console.

    WriteLine

    (

    "Error: "

    &

    ex.

    ToString

    (

    )

    )
  87. Finally
  88. ' Close Connection
  89. Con.

    Close

    (

    )
  90. 'Console.WriteLine("Connection Closed")

  91. End

    Try


  92. End

    Sub


  93. Private

    Sub

    frmRID_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  94. lock(

    )
  95. End

    Sub

  96. Private

    Sub

    cedit_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    cedit.

    Click
  97. If

    cedit.

    Text

    =

    "Update"

    Then

  98. updatesave(

    )
  99. csearch.

    Text

    =

    "Search"
  100. clear(

    )
  101. lock(

    )
  102. ElseIf

    cedit.

    Text

    =

    "Cancel"

    Then
  103. cnew.

    Text

    =

    "New"
  104. cedit.

    Text

    =

    "Update"
  105. lock(

    )
  106. clear(

    )
  107. cdel.

    Enabled

    =

    True
  108. csearch.

    Enabled

    =

    True
  109. End

    If
  110. End

    Sub
  111. Private

    Sub

    updatesave(

    )
  112. Call

    main(

    )
  113. Con.

    Open

    (

    )
  114. Dim

    cmd As

    SqlCommand =

    Con.

    CreateCommand

    (

    )
  115. 'Dim nonqueryCommand As SqlCommand = Con.CreateCommand()

  116. cmd.

    CommandType

    =

    CommandType.

    Text
  117. cmd.

    CommandText

    =

    "update DOrecieve_inventory set lot=@lot,reference=@reference,location=@location,meas=@mas,units=@units,pounds=@pounds where type=@type"

  118. cmd.

    Parameters

    .

    Add

    (

    "@type"

    , SqlDbType.

    VarChar

    , 50

    )
  119. cmd.

    Parameters

    .

    Add

    (

    "@lot"

    , SqlDbType.

    VarChar

    , 50

    )
  120. cmd.

    Parameters

    .

    Add

    (

    "@reference"

    , SqlDbType.

    VarChar

    , 50

    )
  121. cmd.

    Parameters

    .

    Add

    (

    "@location"

    , SqlDbType.

    VarChar

    , 50

    )
  122. cmd.

    Parameters

    .

    Add

    (

    "@mas"

    , SqlDbType.

    VarChar

    , 50

    )
  123. cmd.

    Parameters

    .

    Add

    (

    "@units"

    , SqlDbType.

    VarChar

    , 50

    )
  124. cmd.

    Parameters

    .

    Add

    (

    "@pounds"

    , SqlDbType.

    VarChar

    , 50

    )
  125. ' Prepare command for repeated execution
  126. cmd.

    Prepare

    (

    )

  127. ' Data to be inserted
  128. For

    Each

    row As

    DataGridViewRow In

    grid1.

    Rows
  129. If

    Not

    row.

    IsNewRow

    Then
  130. cmd.

    Parameters

    (

    "@type"

    )

    .

    Value

    =

    row.

    Cells

    (

    0

    )

    .

    Value

    .

    ToString
  131. cmd.

    Parameters

    (

    "@lot"

    )

    .

    Value

    =

    row.

    Cells

    (

    1

    )

    .

    Value

    .

    ToString
  132. cmd.

    Parameters

    (

    "@reference"

    )

    .

    Value

    =

    row.

    Cells

    (

    2

    )

    .

    Value

    .

    ToString
  133. cmd.

    Parameters

    (

    "@location"

    )

    .

    Value

    =

    row.

    Cells

    (

    3

    )

    .

    Value

    .

    ToString
  134. cmd.

    Parameters

    (

    "@mas"

    )

    .

    Value

    =

    row.

    Cells

    (

    4

    )

    .

    Value

    .

    ToString
  135. cmd.

    Parameters

    (

    "@units"

    )

    .

    Value

    =

    row.

    Cells

    (

    5

    )

    .

    Value

    .

    ToString
  136. cmd.

    Parameters

    (

    "@pounds"

    )

    .

    Value

    =

    row.

    Cells

    (

    6

    )

    .

    Value

    .

    ToString
  137. 'MsgBox("update save")
  138. End

    If
  139. Next

  140. If

    Con.

    State

    =

    ConnectionState.

    Closed

    Then
  141. Con.

    Open

    (

    )
  142. End

    If
  143. cmd.

    Connection

    =

    Con
  144. Try
  145. cmd.

    ExecuteNonQuery

    (

    )

    'Then
  146. MsgBox

    (

    "Record Has Updated...!!"

    , MsgBoxStyle.

    Information

    , "UPDATE EXPENDITURE"

    )
  147. Catch

    ex As

    Exception
  148. MsgBox

    (

    ex.

    Message

    )
  149. End

    Try

  150. Con.

    Close

    (

    )
  151. End

    Sub

  152. Private

    Sub

    cdel_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    cdel.

    Click
  153. On

    Error

    GoTo

    err

  154. If

    cdel.

    Text

    =

    "Delete"

    Then
  155. Select

    Case

    MsgBox

    (

    "Are you sure You want to Delete this Record"

    , MsgBoxStyle.

    YesNo

    )
  156. Case

    MsgBoxResult.

    Yes
  157. deleterecord(

    )
  158. csearch.

    Text

    =

    "Search"
  159. clear(

    )
  160. txt1.

    Text

    =

    ""
  161. lock(

    )
  162. grid1.

    Refresh

    (

    )
  163. Case

    MsgBoxResult.

    No
  164. GoTo

    err
  165. End

    Select
  166. End

    If
  167. err:
  168. Exit

    Sub
  169. End

    Sub
  170. Private

    Sub

    deleterecord(

    )
  171. Call

    main(

    )
  172. Con.

    Open

    (

    )
  173. Dim

    adapter As

    New

    SqlDataAdapter

  174. Dim

    cmd As

    SqlCommand =

    Con.

    CreateCommand

    (

    )
  175. cmd.

    CommandType

    =

    CommandType.

    Text
  176. cmd.

    CommandText

    =

    "delete DOrecieve_inventory where type=@type"

  177. cmd.

    Parameters

    .

    Add

    (

    "@type"

    , SqlDbType.

    VarChar

    , 50

    )

  178. 'xxx Prepare command for repeated execution
  179. cmd.

    Prepare

    (

    )

  180. ' Data to be inserted
  181. For

    Each

    row As

    DataGridViewRow In

    grid1.

    Rows
  182. If

    Not

    row.

    IsNewRow

    Then
  183. cmd.

    Parameters

    (

    "@type"

    )

    .

    Value

    =

    row.

    Cells

    (

    0

    )

    .

    Value

    .

    ToString
  184. End

    If
  185. Next

  186. If

    Con.

    State

    =

    ConnectionState.

    Closed

    Then
  187. Con.

    Open

    (

    )
  188. End

    If
  189. cmd.

    Connection

    =

    Con
  190. Try
  191. cmd.

    ExecuteNonQuery

    (

    )

    'Then
  192. MsgBox

    (

    "Record Has Delete..!!"

    , MsgBoxStyle.

    Information

    , "DElete EXPENDITURE"

    )
  193. Catch

    ex As

    Exception
  194. MsgBox

    (

    ex.

    Message

    )
  195. End

    Try

  196. Con.

    Close

    (

    )


  197. End

    Sub

  198. Private

    Sub

    csearch_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    csearch.

    Click


  199. If

    csearch.

    Text

    =

    "Search"

    Then
  200. csearch.

    Text

    =

    "Cancel"
  201. cnew.

    Enabled

    =

    False
  202. unlock(

    )
  203. txt1.

    Focus

    (

    )

  204. ElseIf

    csearch.

    Text

    =

    "Cancel"

    Then
  205. csearch.

    Text

    =

    "Search"
  206. cnew.

    Enabled

    =

    True
  207. 'clear()
  208. txt1.

    Text

    =

    ""
  209. lock(

    )

  210. End

    If


  211. End

    Sub

  212. Private

    Sub

    txt1_TextChanged(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    txt1.

    TextChanged

  213. showauto(

    )
  214. showsearch(

    )

  215. End

    Sub
  216. Private

    Sub

    showauto(

    )
  217. Call

    main(

    )
  218. Con.

    Open

    (

    )

  219. Dim

    cmd As

    New

    SqlCommand(

    "SELECT type FROM DOrecieve_inventory"

    , Con)
  220. Dim

    ds As

    New

    DataSet
  221. Dim

    da As

    New

    SqlDataAdapter(

    cmd)
  222. da.

    Fill

    (

    ds, "My List"

    )

    'list can be any name u want

  223. Dim

    col As

    New

    AutoCompleteStringCollection
  224. Dim

    i As

    Integer
  225. For

    i =

    0

    To

    ds.

    Tables

    (

    0

    )

    .

    Rows

    .

    Count

    -

    1
  226. col.

    Add

    (

    ds.

    Tables

    (

    0

    )

    .

    Rows

    (

    i)

    (

    "type"

    )

    .

    ToString

    (

    )

    )

  227. Next
  228. txt1.

    AutoCompleteSource

    =

    AutoCompleteSource.

    CustomSource
  229. txt1.

    AutoCompleteCustomSource

    =

    col
  230. txt1.

    AutoCompleteMode

    =

    AutoCompleteMode.

    Suggest

  231. Con.

    Close

    (

    )
  232. End

    Sub
  233. Private

    Sub

    showsearch(

    )
  234. Call

    main(

    )
  235. Con.

    Open

    (

    )

  236. Dim

    cmd As

    New

    SqlCommand(

    "select * from DOrecieve_inventory where type like '%"

    +

    txt1.

    Text

    +

    "%'"

    , Con)
  237. Dim

    Adpt As

    New

    SqlDataAdapter(

    cmd)
  238. Dim

    ds As

    New

    DataSet(

    )
  239. If

    (

    Adpt.

    Fill

    (

    ds, "DOrecieve_inventory"

    )

    )

    Then

  240. grid1.

    DataSource

    =

    ds.

    Tables

    (

    0

    )
  241. MessageBox.

    Show

    (

    "match found"

    )

  242. Else
  243. MessageBox.

    Show

    (

    "match not found"

    )
  244. End

    If
  245. Con.

    Close

    (

    )
  246. End

    Sub


  247. End

    Class

 

452,496

338,535

338,543

Top