[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to write a function which checks for the data value and returns a boolean value

Posted on 2014-08-27
3
Medium Priority
?
258 Views
Last Modified: 2014-08-28
Hi Everyone,

I am trying to write a function in c# and I have a query in it and the function should return true if the data value is not null?

How can I write this function in C#?

Thanks,
D.
0
Comment
Question by:developer2012
  • 2
3 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 40289492
so you have to query a database? please post your code so far.
0
 

Author Comment

by:developer2012
ID: 40290414
Here is my code

  public bool CheckID(string attnum)
        {
            string _depid = null;
            if (!(attnum == null))
            {
                string ConString = "User Id=t;Password=t;Data Source=T";
                using (OracleConnection con = new OracleConnection(ConString))
                {
                    OracleDataAdapter adapter = new OracleDataAdapter();
                    con.Open();
                    OracleCommand command = new OracleCommand("Select ID from Employees Where ID > 0 AND ID Is not NULL AND Attr_Num = '" + attnum + "' ", con);

                    adapter.SelectCommand = command;
                    DataSet dataset = new DataSet("ATTID");
                    adapter.Fill(dataset);
                    foreach (DataRow dr in dataset.Tables[0].Rows)
                    {
                        _depid = dr["ID"].ToString();
                    }
                    con.Close();
                }
                if(!(_depid == null))
                 return true
            }
           
        }

Open in new window


And the above code throws an error ' The code function path does not return any value'
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 40290484
You don't need the data adapter, only ExecuteScalar.

        public bool CheckID(string attnum)
        {
            object ret = null;
            if (attnum != null)
            {
                string ConString = "User Id=t;Password=t;Data Source=T";
                using (OracleConnection con = new OracleConnection(ConString))
                {
                    con.Open();
                    OracleCommand command = new OracleCommand("Select ID from Employees Where ID > 0 AND ID Is not NULL AND Attr_Num = '" + attnum + "' ", con);

                    ret = command.ExecuteScalar();
                }
            }
            return ret != null;
        }

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses
Course of the Month19 days, 22 hours left to enroll

873 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