Datareader(simple)

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

    #1

    Datareader(simple)

    SqlCommand findProducts = new SqlCommand("SEL ECT PRODUCT_TYPE_ID FROM
    PRODUCT_TYPE WHERE [NAME] = 'Meals'", sqlConnection1) ;

    This command returns a value of 17. How can i insert this value into
    a string variable? Please help . I tried this but it wouldnt work

    SqlDataReader myReader = findProducts.Ex ecuteReader();
    while (myReader.Read( ))
    {
    ProdTypeID =
    int.Parse(myRea der["PRODUCT_TYPE_I D"].ToString());
    }
  • Marc Gravell

    #2
    Re: Datareader(simp le)

    If it only returns a single cell, then ExecuteScalar() might be an
    easier option; since you state that you want a string, the easiest
    option would then be:

    string foo = Convert.ToStrin g(findProducts. ExecuteScalar() );

    How to best use data-reader depends on whether PRODUCT_TYPE_ID is an int
    or a [n][var]char;
    if int:
    string foo = reader.GetInt32 (0).ToString();
    if [n][var]char:
    string foo = reader.GetStrin g(0);

    You could also use:
    string foo = Convert.ToStrin g(reader[0]);
    or
    string foo = Convert.ToStrin g(reader["PRODUCT_TYPE_I D"]);

    Marc

    Comment

    Working...