Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi ...

Iam getting this error :

CS1955	Non-invocable member 'SqlCommand.Parameters' cannot be used like a method.


And

CS0103	The name 'Conversions' does not exist in the current context


This Is my class :

C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Project5.NewFolder1
{
    class DatabaseManager
    {
        private SqlConnection _connection;
        private readonly string _connectionString;
        public SqlConnection Connection
        {
            get
            {
                if (_connection is null)
                    _connection = new SqlConnection(_connectionString);
                return _connection;
            }
        }

        public DatabaseManager()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["connSQLServer"].ConnectionString;
        }

        public DatabaseManager(string ConnectionString)
        {
            this._connectionString = _connectionString;
        }

        public void Close()
        {
            if (_connection is object && _connection.State == ConnectionState.Open)

                _connection.Close();
        }

        public int FillTable(ref SqlCommand cmd, ref DataTable dt)
        {
            int retval = -1;
            dt = new DataTable();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Connection;
            var da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            if (dt is object)
                retval = dt.Rows.Count;
            return retval;
        }

        public int ExecuteReader(ref SqlCommand cmd, ref SqlDataReader dr)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            return ExeReader(ref cmd, ref dr);
        }

        public int ExecuteReader(string sql, ref SqlDataReader dr)
        {
            var cmd = new SqlCommand(sql)
            {
                CommandType = CommandType.Text,
                Connection = Connection
            };
            int retval = ExecuteReader(ref cmd, ref dr);
            return retval;
        }

        private int ExeReader(ref SqlCommand cmd, ref SqlDataReader dr)
        {
            int retval = -1;
            cmd.Connection = Connection;
            try
            {
                if (cmd.CommandType == CommandType.StoredProcedure)
                {
                    var pr = new SqlParameter("@retval", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue };
                    cmd.Parameters.Add(pr);
                }

                if (cmd.Connection.State == ConnectionState.Closed)
                    cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                if (cmd.CommandType == CommandType.StoredProcedure)
                    retval = cmd.Parameters("@retval").Value;   
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Close();
            }

            return retval;
        }
        public int ExecuteNonQuery(ref SqlCommand cmd)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            return ExeNonQuery(ref cmd);
        }

        public int ExecuteNonQuery(ref string sql)
        {
            var cmd = new SqlCommand(sql)
            {
                CommandType = CommandType.Text,
                Connection = Connection
            };
            int retval = ExeNonQuery(ref cmd);
            return retval;
        }

        private int ExeNonQuery(ref SqlCommand cmd)
        {
            int retval = -1;
            cmd.Connection = Connection;
            try
            {
                if (cmd.CommandType == CommandType.StoredProcedure)
                {
                    var pr = new SqlParameter("@retval", SqlDbType.Int) { Direction = ParameterDirection.ReturnValue };
                    cmd.Parameters.Add(pr);
                }

                if (cmd.Connection.State == ConnectionState.Closed)
                    cmd.Connection.Open();
                retval = cmd.ExecuteNonQuery();
                if (cmd.CommandType == CommandType.StoredProcedure)

                    retval = cmd.Parameters("@retval").Value;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Close();
            }

            return retval;
        }
        public object ExecuteScalar(ref SqlCommand cmd)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            return ExeScalar(ref cmd);
        }

        public object ExecuteScalar(string sql)
        {
            var cmd = new SqlCommand(sql)
            {
                CommandType = CommandType.Text,
                Connection = Connection
            };
            int retval = Conversions.ToInteger(ExecuteScalar(ref cmd));
            return retval;
        }

        private object ExeScalar(ref SqlCommand cmd)
        {
            object retval;
            cmd.Connection = Connection;
            try
            {
                if (cmd.Connection.State == ConnectionState.Closed)
                    cmd.Connection.Open();
                retval = cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Close();
            }

            return retval;
        }
    }

}



Thanks ....

What I have tried:

What causes these error ?

<pre>CS1955	Non-invocable member 'SqlCommand.Parameters' cannot be used like a method.


And

CS0103	The name 'Conversions' does not exist in the current context
Posted
Updated 23-Mar-20 7:32am

Look at your code:
C#
if (cmd.CommandType == CommandType.StoredProcedure)
   retval = cmd.Parameters("@retval").Value;  

Parameters is a collection, not a method:
C#
if (cmd.CommandType == CommandType.StoredProcedure)
   retval = cmd.Parameters["@retval"].Value;  


[edit]
And it's "Convert" not "Conversions"
[/edit]
 
Share this answer
 
v3
Thanks for replay friend

yes i tried to change the brakes to ["@retval"]

but still the error is there

about the other error is fixed by change the code to

int retval = Convert.ToInt32(ExecuteScalar(ref cmd));
 
Share this answer
 

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