Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Please help with Inserting value from DataGrid in Database Table

Please help with Inserting value from DataGrid in Database Table

Scheduled Pinned Locked Moved Visual Basic
helpcsharpdatabaseoraclesysadmin
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hammad Mansoor
    wrote on last edited by
    #1

    Hi all I'm trying to get a value of a column in DataGrid and insert it in Oracle Table. Below is my code. Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting Dim oradb As String = "Data Source=(DESCRIPTION=" _ + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=Mahasilbkp.ltuisb.net)(PORT=1521)))" _ + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=db21a)));" _ + "User Id=" + Label5.Text + ";Password=;pooling=false;enlist=false;" Dim conn1 As New OracleConnection(oradb) conn1.Open() Dim cmd1 As New OracleCommand cmd1.Connection = conn1 GridView1.DataBind() Dim val As Label = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label) cmd1.CommandText = "insert into uu.record_backup(ntn, name, status, enf, aud, sector, delete_user) select ntn, name, status, enf, aud, sector" + ",'" + Label5.Text + "'" + " " + "from uu.total_cases where trim(ntn)='" + val.Text.ToString + "'" Dim dr1 As OracleDataReader = cmd1.ExecuteReader() dr1.Read() dr1.Dispose() dr1.Close() cmd1.Dispose() conn1.Dispose() conn1.Close() End Sub When I click on Delete it gives the following error: Object reference not set to an instance of an object. Source Error: Line 49: cmd1.CommandText = "insert into uu.record_backup(ntn, name, status, enf, aud, sector, delete_user) select ntn, name, status, enf, aud, sector" + ",'" + Label5.Text + "'" + " " + "from uu.total_cases where trim(ntn)='" + val.Text + "'" If I remove the "val.Text" from the above code then it works, but obviously the results are not as I desire. Please help me with this. Thanks.

    T 1 Reply Last reply
    0
    • H Hammad Mansoor

      Hi all I'm trying to get a value of a column in DataGrid and insert it in Oracle Table. Below is my code. Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting Dim oradb As String = "Data Source=(DESCRIPTION=" _ + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=Mahasilbkp.ltuisb.net)(PORT=1521)))" _ + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=db21a)));" _ + "User Id=" + Label5.Text + ";Password=;pooling=false;enlist=false;" Dim conn1 As New OracleConnection(oradb) conn1.Open() Dim cmd1 As New OracleCommand cmd1.Connection = conn1 GridView1.DataBind() Dim val As Label = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label) cmd1.CommandText = "insert into uu.record_backup(ntn, name, status, enf, aud, sector, delete_user) select ntn, name, status, enf, aud, sector" + ",'" + Label5.Text + "'" + " " + "from uu.total_cases where trim(ntn)='" + val.Text.ToString + "'" Dim dr1 As OracleDataReader = cmd1.ExecuteReader() dr1.Read() dr1.Dispose() dr1.Close() cmd1.Dispose() conn1.Dispose() conn1.Close() End Sub When I click on Delete it gives the following error: Object reference not set to an instance of an object. Source Error: Line 49: cmd1.CommandText = "insert into uu.record_backup(ntn, name, status, enf, aud, sector, delete_user) select ntn, name, status, enf, aud, sector" + ",'" + Label5.Text + "'" + " " + "from uu.total_cases where trim(ntn)='" + val.Text + "'" If I remove the "val.Text" from the above code then it works, but obviously the results are not as I desire. Please help me with this. Thanks.

      T Offline
      T Offline
      TheComputerMan
      wrote on last edited by
      #2

      val.Text.ToString ?? What is the object val ?? If it is a text box, try naming it to something else.

      H 1 Reply Last reply
      0
      • T TheComputerMan

        val.Text.ToString ?? What is the object val ?? If it is a text box, try naming it to something else.

        H Offline
        H Offline
        Hammad Mansoor
        wrote on last edited by
        #3

        val is a declared label to store the data from the NTN column of the grid Dim val As Label = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label) Thanks.

        T J 2 Replies Last reply
        0
        • H Hammad Mansoor

          val is a declared label to store the data from the NTN column of the grid Dim val As Label = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label) Thanks.

          T Offline
          T Offline
          TheComputerMan
          wrote on last edited by
          #4

          I suspected that. Probably not a good idea to have a label with the same name as a VB function. I am not saying that this is the cause of your problem, but try renaming that label first. Dim lblNTN As Label would be much more readable. Actually looking at this, where is this label being created?

          Dim val As Label = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label)

          What is control "NTN"?

          1 Reply Last reply
          0
          • H Hammad Mansoor

            val is a declared label to store the data from the NTN column of the grid Dim val As Label = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label) Thanks.

            J Offline
            J Offline
            Johan Hakkesteegt
            wrote on last edited by
            #5

            As TheComputerGuy said and then try:

            Dim lblNTN As New Label
            lblNTN = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label)

            My advice is free, and you may get what you paid for.

            H 1 Reply Last reply
            0
            • J Johan Hakkesteegt

              As TheComputerGuy said and then try:

              Dim lblNTN As New Label
              lblNTN = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label)

              My advice is free, and you may get what you paid for.

              H Offline
              H Offline
              Hammad Mansoor
              wrote on last edited by
              #6

              Ok guys i tried that, but I am still getting that "Object reference not set to an instance of an object" on the insert statement. Just want to clarify that "NTN" in lblNTN = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label) is the name of the column of the Data Grid from which I want the data to be retrieved. Am I doing it the correct way? If not please help with this. Thanks.

              J 1 Reply Last reply
              0
              • H Hammad Mansoor

                Ok guys i tried that, but I am still getting that "Object reference not set to an instance of an object" on the insert statement. Just want to clarify that "NTN" in lblNTN = DirectCast((GridView1.Rows(e.RowIndex).FindControl("NTN")), Label) is the name of the column of the Data Grid from which I want the data to be retrieved. Am I doing it the correct way? If not please help with this. Thanks.

                J Offline
                J Offline
                Johan Hakkesteegt
                wrote on last edited by
                #7

                You might as well just forget about the label completely, and try something like this:

                cmd1.CommandText = "insert into uu.record_backup(ntn, name, status, enf, aud, sector, delete_user) select ntn, name, status, enf, aud, sector" _
                & ",'" & Label5.Text & "'" & " from uu.total_cases where trim(ntn)='" _
                & GridView1.Rows(e.RowIndex).Cells("NTN").Value & "'"

                My advice is free, and you may get what you paid for.

                H 1 Reply Last reply
                0
                • J Johan Hakkesteegt

                  You might as well just forget about the label completely, and try something like this:

                  cmd1.CommandText = "insert into uu.record_backup(ntn, name, status, enf, aud, sector, delete_user) select ntn, name, status, enf, aud, sector" _
                  & ",'" & Label5.Text & "'" & " from uu.total_cases where trim(ntn)='" _
                  & GridView1.Rows(e.RowIndex).Cells("NTN").Value & "'"

                  My advice is free, and you may get what you paid for.

                  H Offline
                  H Offline
                  Hammad Mansoor
                  wrote on last edited by
                  #8

                  Ok guys "GridView1.Rows(e.RowIndex).Cells("NTN").Value" gives error on the "value" as it does not recognize it. So I used "GridView1.Rows(e.RowIndex).Cells("NTN").Text". Now I am getting the following error System.FormatException: Input string was not in a correct format. it is coming on this line. Line 54: cmd1.CommandText = "insert into uu.record_backup(ntn, name, status, enf, aud, sector, delete_user) select ntn, name, status, enf, aud, sector,'" + Label5.Text + "' " + "from uu.total_cases where trim(ntn)='" + GridView1.Rows(e.RowIndex).Cells("NTN").Text + "'" Please help me where I am making mistake in the above line. Thanks for your help.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups