Solved c# sqldatareader compare value with String

Posted on 2009-02-13
Last Modified: 2013-12-17

I have this code that gets the fields from the database

SqlCommand gories = new SqlCommand("sp_writs", oCottoNwsCateies);
       NewsCategories.CommandType = CommandType.StoredProcedure;

        SqlDataReader rdrNewsCategories = NewsCategories.ExecuteReader();

so this code is fine.

but I have a coloum in the database caled write which I want to compare to a string called dontwrite

so I want to do is
if write is"page" and page is not equal to dont write then insert otherwise ignore

please help



Question by:R8VI
    LVL 6

    Accepted Solution

    See code snippet

    while (rdrNewsCategories.Read())
         if ((dr["write"].ToString() == "page") && 
             (dr["page"].ToString () !   = "dontwrite"))
             // perform your insert logic

    Open in new window

    LVL 6

    Expert Comment

    Somehow in my code snippet on line 4 what should be != has spaces in it.
    LVL 10

    Expert Comment

    this could also be an SQL reserved word issue... in that case, try Jammer's option above, and use this:



    write could be an SQL transaction word like DROP or SELECT... so your field name may be in conflict in the SQL query, thus you,re getting nothing in that field?

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now