basic dataadapter/dataset question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie

    #1

    basic dataadapter/dataset question

    Hi,

    I have a dataadapter that contains just a single record.

    Dim SqlDataAdapterV alues As New SqlDataAdapter( "select * from
    tbl_formatvalue where format_id = " & intFormatInt, dbConnectionIn)
    SqlDataAdapterJ obs.Fill(dsForm ats, "formatvalu es")

    I want to get the value for column customer_id from this dataset.

    usually i would have a dataset with many records, and just do a for each
    ..... looping through and processing each row.

    In this case, as there is only one record in the dataset, is there a better
    way to get to the data that using the for each?

    Or.. is there a better way of getting my data, is a dataset in this
    situation not the best.


    Thanks



  • sloan

    #2
    Re: basic dataadapter/dataset question


    DataSets do not do "singles" very well.

    If you create a strong dataset.....you can get at the row like this
    (sorry I can only do c# from memory)

    Let's say I have a strong dataset called OrganizationDS
    and I have "Employee" as a table. EmpID (int) adn SSN (string)

    OrganizationDS ds = new OrganizationDS( );
    ds.Employee.Add NewEmployeeRow ( 101 , "222334444" );

    if (null!=ds)
    {
    if(null!= ds.Employee)
    {
    if(ds.Employee. Count>0) //<<this might be the Length property
    OrganizationDS. EmployeeRow row = ds.Employee[0];
    if(null!= row)
    {
    Console.WriteLi ne ( row.EmpID ) ;
    Console.WriteLi ne ( row.SSN ) ;

    }

    //also use the Select
    DataRow[] rows = ds.Employee.Sel ect("EmpID=101" );
    if (null!=rows)
    {
    if(rows.Length> 0)
    {
    OrganizationDS. EmployeeRow anotherRow = rows[0] as
    OrganizationDS. EmployeeRow;
    if(null!= anotherRow )
    {
    Console.WriteLi ne ( anotherRow .EmpID ) ;
    Console.WriteLi ne ( anotherRow .SSN ) ;

    }

    }
    }

    }
    }




    Here is a GREAT article on different approaches:
    http://msdn.microsoft.com/en-us/library/ms978496.aspx
    Bookmark it, read it every 3 months (for 2 years) since you are new.
    It wont' "take" the first time, but will make more and more sense as you
    develop.

    Which is where you will read this:
    Passing DataSets As Inputs and Outputs
    The disadvantages of this option are as follows:
    * Representation of a single business entity


    "Newbie" <newbie@nospam. comwrote in message
    news:OufnIMvQJH A.1160@TK2MSFTN GP02.phx.gbl...
    Hi,
    >
    I have a dataadapter that contains just a single record.
    >
    Dim SqlDataAdapterV alues As New SqlDataAdapter( "select * from
    tbl_formatvalue where format_id = " & intFormatInt, dbConnectionIn)
    SqlDataAdapterJ obs.Fill(dsForm ats, "formatvalu es")
    >
    I want to get the value for column customer_id from this dataset.
    >
    usually i would have a dataset with many records, and just do a for each
    .... looping through and processing each row.
    >
    In this case, as there is only one record in the dataset, is there a
    better way to get to the data that using the for each?
    >
    Or.. is there a better way of getting my data, is a dataset in this
    situation not the best.
    >
    >
    Thanks
    >
    >
    >

    Comment

    • Rich P

      #3
      Re: basic dataadapter/dataset question

      Here is how you perform a search on a table in a dataset

      Dim drF() As DataRow
      drF = dsMain.Tables(" MyTable").Selec t("ID = " & strID)

      For Each row As Datarow in drF
      console.writeli ne(row("ID").to String & " " & row("Name"))
      Next

      you can replace the .Select("ID = ...
      with .Select("FirstD ate = '" & someStringDate & "'")

      The .Select property takes a regular string for the Where part of a sql
      statement.

      .Select("ID = 1")

      .Select("ID = " & someIntVar.ToSt ring)


      Rich

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Branco Medeiros

        #4
        Re: basic dataadapter/dataset question

        Newbie wrote:
        <snip>
        I have a dataadapter that contains just a single record.
        >
        Dim SqlDataAdapterV alues As New SqlDataAdapter( "select * from
        tbl_formatvalue where format_id = " & intFormatInt, dbConnectionIn)
        SqlDataAdapterJ obs.Fill(dsForm ats, "formatvalu es")
        >
        I want to get the value for column customer_id from this dataset.
        <snip>

        If you only want one customer_id from the query, you could use the
        ExecuteScalar method of the SqlCommand:

        <example>
        'Prepare the command
        Dim Cmd As New SqlCommand( _
        "select top 1 customer_id " _
        & "from tbl_formatvalue " _
        & "where format_id = @format_id")

        Cmd.Parameters. Add("@format_id ")
        Cmd.Connection = dbConnectionIn

        '... elsewhere in your code

        'specify the parameter value
        Cmd.Parameters( "@format_id").V alue = intFormatInt

        'fetch the data ad convert the result to integer
        Dim CustID As Integer = Ctype(Cmd.Execu teScalar, Integer)
        </example>

        Of course, you could build a class to encapsulate all that and have a
        method to return the ID already converted to integer and where access
        errors etc, would be dealt with...

        Dim CustID As Integer = SomeClass.GetCu stomerID(intFor matInt)


        Hope this helps.

        Regards,

        Branco.

        Comment

        Working...