Link to home
Start Free TrialLog in
Avatar of Anushart
AnushartFlag for United States of America

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(strSQL, oConn)

            Dim dr As DataRow
            dr = DTtrackingno.NewRow
            temp(0) = oCmd.ExecuteScalar.ToString

            '****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,
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What is the exception?

Bob
Avatar of dstanley9
dstanley9

           temp(0) = oCmd.ExecuteScalar.ToString

            '****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.ToString
            dr.Item(0) = temp
Avatar of Anushart

ASKER

Column 0 not found was the exception
Does the data row have any columns?
dim temp as object  /Declared Globally


           temp = oCmd.ExecuteScalar.ToString
            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.
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
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
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()?
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(dc1)

    End Sub
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

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
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah, that's what I said, too.  Too bad!!   No recognition!!!

Bob