Problem with reader

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

    #1

    Problem with reader

    hi all.
    this is my code

    %@ Import Namespace="Syst em.Data" %>
    <%@ Import Namespace="Syst em.Data.SqlClie nt" %>
    <script language="C#" runat="server">
    void Page_Load(Objec t sender, EventArgs e)
    {
    SqlConnection myConn2 = new SqlConnection(" server =localhost;
    UID=***;PWD=*** ;database=forum ");
    SqlCommand myComm2 = new SqlCommand("SEL ECT Replace(body,ch ar(13),'<br>')
    from Posts",myConn2) ;
    myConn2.Open();
    SqlDataReader myRead = myComm2.Execute Reader();
    while(myRead.Re ad())
    {
    Response.Write( myRead["Body"]);
    }
    myRead.Close();
    myConn2.Close() ;
    }

    So, when i try execute this code i return error

    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.IndexOut OfRangeExceptio n: Body

    Source Error:

    Line 10: while(myRead.Re ad())
    Line 11: {
    Line 12: Response.Write( myRead["Body"]);
    Line 13: }
    Line 14: myRead.Close();


    Any idea?

    P.S.

    If i use datareader everything is ok.

    Thanks.


  • Kevin Spencer

    #2
    Re: Problem with reader

    You didn't alias the field. You aren't selecting the "Body" field from the
    table. You're creating a new derived column from it. Use an alias for the
    column:

    SELECT Replace(body,ch ar(13),'<br>') As Body from Posts

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    All hotels in Benidorm. The best selection of Benidorm hotels with reviews and maps. Book in advance and save.

    Neither a follower nor a lender be.

    "Dmitri Manushin" <dm-man@mail.ru> wrote in message
    news:eubpxUwYDH A.1384@TK2MSFTN GP10.phx.gbl...[color=blue]
    > hi all.
    > this is my code
    >
    > %@ Import Namespace="Syst em.Data" %>
    > <%@ Import Namespace="Syst em.Data.SqlClie nt" %>
    > <script language="C#" runat="server">
    > void Page_Load(Objec t sender, EventArgs e)
    > {
    > SqlConnection myConn2 = new SqlConnection(" server =localhost;
    > UID=***;PWD=*** ;database=forum ");
    > SqlCommand myComm2 = new SqlCommand("SEL ECT[/color]
    Replace(body,ch ar(13),'<br>')[color=blue]
    > from Posts",myConn2) ;
    > myConn2.Open();
    > SqlDataReader myRead = myComm2.Execute Reader();
    > while(myRead.Re ad())
    > {
    > Response.Write( myRead["Body"]);
    > }
    > myRead.Close();
    > myConn2.Close() ;
    > }
    >
    > So, when i try execute this code i return error
    >
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.IndexOut OfRangeExceptio n: Body
    >
    > Source Error:
    >
    > Line 10: while(myRead.Re ad())
    > Line 11: {
    > Line 12: Response.Write( myRead["Body"]);
    > Line 13: }
    > Line 14: myRead.Close();
    >
    >
    > Any idea?
    >
    > P.S.
    >
    > If i use datareader everything is ok.
    >
    > Thanks.
    >
    >[/color]


    Comment

    Working...