Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I have written a code in C# to fetch the data from database.
C#
public string GetDatabaseValues(string value)
        {
            string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["RestAPIConnectionString"].ConnectionString;
            string str = "";
            using (SqlConnection myConnection = new SqlConnection(strCon))
            {

                using (SqlCommand cmd = new SqlCommand(@"select Result from Output where Moviename=@0", myConnection))
                {
                    try
                    {
                        myConnection.Open();
                        cmd.Parameters.AddWithValue("@0", value);
                        str = cmd.ExecuteScalar().ToString();


                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        myConnection.Close();
                        myConnection.Dispose();
                    }
                }
            }
            return str;
        }


My database has the values. The problem here is despite my database containing proper values, the ExecuteScalar command is always sending null value and this results in "Object Reference set to null" error.

Please help in finding out the problem in my code or atleast let me know any alternative to ExecuteScalar command.

What I have tried:

tried Execute.Nonquery but failed.
Posted
Updated 22-Mar-17 20:33pm

The only thing I can think of here is perhaps your sql provider does not like a numeric parameter name. Try renaming "@0" to "@p0", and see if it makes a difference.
Otherwise the code looks fine to me.
 
Share this answer
 
Comments
Karthik_Mahalingam 23-Mar-17 3:13am    
Hi Mick
tested with "@0", it works good.
use Convert.ToString [^] instead of ToString[^]
str  = Convert.ToString( cmd.ExecuteScalar());

if there is no result for the given condition cmd.ExecuteScalar() will retun null value and if you try to access a member of a null object which will obviously result in Object reference error

read this c# - Difference between Convert.ToString() and .ToString() [^]
 
Share this answer
 
v3
Comments
Dinesh Kumar Dora 23-Mar-17 2:16am    
Hello Karthik,

Thanks for the response. As suggested by you, i tried Convert.ToString. Now instead of null, its sending "". But rest assured, in the database, value is present. why its not returning the value.
Karthik_Mahalingam 23-Mar-17 2:22am    
run this query in sql studio and check what you are getting
select Result from Output where Moviename= value
Dinesh Kumar Dora 23-Mar-17 2:20am    
The strangest part is.. The database has many values. This code is always sending null for all the values present in the DB, Except for one particular value. If i send the ID of this particular value, i always get the exact corresponding value of this particular ID. But the same does'nt work for other values. All the values are in text format and i do'nt see any format issues which can create this situation.
Karthik_Mahalingam 23-Mar-17 2:22am    
try select top 1 Result from Output where Moviename= value
Dinesh Kumar Dora 23-Mar-17 2:25am    
No change in behavior

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900