Anushart
asked on
How to add retrived object from oCmd.ExecuteScalar.ToString as a datarow element
Public Sub saveData(ByVal strConn As String, ByVal strSQL As String)
Dim oCmd As New SqlClient.SqlCommand
Dim oAdapt As New SqlClient.SqlDataAdapter
Dim oConn As New SqlClient.SqlConnection
Try
oConn.ConnectionString = strConn
oConn.Open()
oCmd = New SqlClient.SqlCommand(strSQ L, oConn)
Dim dr As DataRow
dr = DTtrackingno.NewRow
temp(0) = oCmd.ExecuteScalar.ToStrin g
'****I need to assign this temp object as data row below is the code I wrote but its giving me error
dr.Item(0) = temp
DTtrackingno.Rows.Add(dr)
DTtrackingno.AcceptChanges ()
Catch ex As Exception
Throw New Exception("Error in getData()- Datatable" & ex.Message)
Finally
If oConn.State = ConnectionState.Open Then
oConn.Close()
oConn.Dispose()
End If
End Try
End Sub
Above is my code and I have marked *** at the place I am getting the error.
All I need to do is add the retrived object to a datarow of data table DTtrackingno.
Thanks,
Dim oCmd As New SqlClient.SqlCommand
Dim oAdapt As New SqlClient.SqlDataAdapter
Dim oConn As New SqlClient.SqlConnection
Try
oConn.ConnectionString = strConn
oConn.Open()
oCmd = New SqlClient.SqlCommand(strSQ
Dim dr As DataRow
dr = DTtrackingno.NewRow
temp(0) = oCmd.ExecuteScalar.ToStrin
'****I need to assign this temp object as data row below is the code I wrote but its giving me error
dr.Item(0) = temp
DTtrackingno.Rows.Add(dr)
DTtrackingno.AcceptChanges
Catch ex As Exception
Throw New Exception("Error in getData()- Datatable" & ex.Message)
Finally
If oConn.State = ConnectionState.Open Then
oConn.Close()
oConn.Dispose()
End If
End Try
End Sub
Above is my code and I have marked *** at the place I am getting the error.
All I need to do is add the retrived object to a datarow of data table DTtrackingno.
Thanks,
temp(0) = oCmd.ExecuteScalar.ToStrin g
'****I need to assign this temp object as data row below is the code I wrote but its giving me error
dr.Item(0) = temp
You're setting up temp as an array then trying to assign it to dr.Item(0).
Try
temp = oCmd.ExecuteScalar.ToStrin g
dr.Item(0) = temp
'****I need to assign this temp object as data row below is the code I wrote but its giving me error
dr.Item(0) = temp
You're setting up temp as an array then trying to assign it to dr.Item(0).
Try
temp = oCmd.ExecuteScalar.ToStrin
dr.Item(0) = temp
ASKER
Column 0 not found was the exception
Does the data row have any columns?
ASKER
dim temp as object /Declared Globally
temp = oCmd.ExecuteScalar.ToStrin g
dr.Item(0) = temp
DTtrackingno.Rows.Add(dr)
Hello dstanley9 I tried the above code as said but still its givving me belowerror
DatatableCannot find column 0.
temp = oCmd.ExecuteScalar.ToStrin
dr.Item(0) = temp
DTtrackingno.Rows.Add(dr)
Hello dstanley9 I tried the above code as said but still its givving me belowerror
DatatableCannot find column 0.
ASKER
No I did not create any colums as my query returns only one item which has to be assined as a row value
How is DTtrackingno defined? If it is a memory DataTable, did you add DataColumns to it?
Bob
Bob
Well if DTtrackingno does not have any columns then the datarow created with NewRow() will not either. You'll need to define a data column for DTtrackingno in order to add values to the new row.
Example:
Dttrackingno.Columns.Add(" Item1", GetType(String))
Bob
Dttrackingno.Columns.Add("
Bob
ASKER
Bob if I write this code Dttrackingno.Columns.Add(" Item1", GetType(String)) its gonna create a column every time when I call that function.Can you suggest me if I can write that code in page load
I'd do it whenever you are initializing Dttrackingno. Are you just initializing is as a new DataTable()?
ASKER
Bob I worte that code in page load & its working fine ...I'm a new developer can you suggest me if that declaration in page load is ok
This is how I declared
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dc1 As DataColumn
'dc1 = New DataColumn("TrackingNumber ", Type.GetType(" System.String"), "")
DTtrackingno.Columns.Add(d c1)
End Sub
This is how I declared
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dc1 As DataColumn
'dc1 = New DataColumn("TrackingNumber
DTtrackingno.Columns.Add(d
End Sub
ASKER
Stanley I declared
Dim Dttrackingno as New Datatable globally
and Declared a column called Trackingno for it in pageload as shown in above comment and its working fine ... Can you [lease tell me if this is fine
Dim Dttrackingno as New Datatable globally
and Declared a column called Trackingno for it in pageload as shown in above comment and its working fine ... Can you [lease tell me if this is fine
ASKER
Thanks guys its time for me to go home.Jst let me know abt my last comment and I'll reply you tommorow.
Thankyou once again
Thankyou once again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yeah, that's what I said, too. Too bad!! No recognition!!!
Bob
Bob
Bob