Please help with Inserting value from DataGrid in Database Table
-
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.
-
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.
val.Text.ToString ?? What is the object val ?? If it is a text box, try naming it to something else.
-
val.Text.ToString ?? What is the object val ?? If it is a text box, try naming it to something else.
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.
-
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.
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"?
-
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.
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.
-
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.
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.
-
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.
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.
-
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.
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.