sql data set question with 2.0

Posted on 2006-05-08
Last Modified: 2011-09-20
I need to determine if user row exists and determine if cell phone number exists.
1. if cell number = 0 then false
2. if row does not exist then false

I know how to check if the row exists, but how do I check the value of a column?

protected bool HasMobileNumber(string strUserID)
  //query table to determine presense of user cellphone number per userid
  String strConnection;
  strConnection = ConfigurationManager.ConnectionStrings["MYConnectionString"].ToString();
  SqlConnection objConnection = new SqlConnection(strConnection);
  string strSQL = "";
  DataSet objDataSet = new DataSet();
  strSQL = "Select userid, cellnumber, CASE WHEN ISNULL(cellnumber, '') != '' THEN 1 ELSE 0 END hascellnumber from usracct where user ='" + strUserID + "'";
  SqlDataAdapter objAdapter = new SqlDataAdapter(strSQL, objConnection);
  objAdapter.Fill(objDataSet, "dtUser_Logins");
//if hascellnumber in ds is 0 or (objDataSet.Tables["dtUser_Logins"].Rows.Count == 0)
//return false;
//return true;
Question by:fwsteal
    LVL 52

    Expert Comment

    by:Carl Tawn

        strSQL = "Select userid, cellnumber, CASE WHEN ISNULL(cellnumber, '') != '' THEN 1 ELSE 0 END hascellnumber from usracct where user ='" + strUserID + "' AND cellnumber != '0'";

    Author Comment

    wouldn't the cellnumber need to be ''

    this way a row won't be returned?
    LVL 52

    Expert Comment

    by:Carl Tawn
    Do you simply want to stop it returning any records with no cellnumber, or retireve them anyway and check afterwards ?

    Author Comment

    I guess, what I'm trying to do is not return a row if both conditions are false so this way I can just check if a row exists
    LVL 52

    Accepted Solution

    If you just use:
        strSQL = "SELECT userid, cellnumber FROM usracct WHERE user='" + strUserID + "' AND cellnumber IS NOT NULL"

    Then you will not get any rows if the row doesn't exist, or if the cellnumber is not filled in. Then the test for your "if" simply becomes:

        if (objDataSet.Tables["dtUser_Logins"].Rows.Count == 0)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now