Skip to main content
Uso correto do bloco para código para melhor compreensão
Fonte Link
    using System;

using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web;

namespace Roger_Silva_DR2_AT.DAL { public class DataAccessLayer {

 using System.Collections.Generic;
using System.Configuration;
using publicSystem.Data;
using stringSystem.Data.SqlClient;
using InserirDados(UsuarioSystem.Linq;
using objuser)System.Web;

namespace Roger_Silva_DR2_AT.DAL
{
    public class DataAccessLayer
    {
        SqlConnection con = null;

        string result = "";

        trypublic string InserirDados(Usuario objuser)

        {
            SqlConnection con = null;

            constring =result new= SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());"";

            SqlCommand cmd = new SqlCommand("Usp_InserirAtulizarDeletar_Usuario", con)try

            {
                CommandType = CommandType.StoredProcedure
            };

            cmd    con = new SqlConnection(ConfigurationManager.ParametersConnectionStrings["mycon"].AddWithValueToString("@ID", 0));

                SqlCommand cmd.Parameters.AddWithValue = new SqlCommand("@Nome""Usp_InserirAtulizarDeletar_Usuario", objuser.Nomecon)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@Sobrenome""@ID", objuser.Sobrenome0);

                cmd.Parameters.AddWithValue("@Nascimento""@Nome", objuser.NascimentoNome);

                cmd.Parameters.AddWithValue("@Query""@Sobrenome", 1objuser.Sobrenome);

            con    cmd.OpenParameters.AddWithValue("@Nascimento", objuser.Nascimento);

            result =   cmd.ExecuteScalar()Parameters.ToStringAddWithValue("@Query", 1);

            return result;   con.Open();

        }        result = cmd.ExecuteScalar().ToString();

        catch        return result;

        {    }

            return result = "";catch

        }    {

        finally        return result = "";

        {    }

            con.Close();finally

        }    {

    }            con.Close();

    public string AtualizarDados(Usuario objuser)     }

    {    }

        SqlConnectionpublic constring =AtualizarDados(Usuario null;objuser)

        string result = "";{

        try    SqlConnection con = null;

        {    string result = "";

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());try

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd    con = new SqlConnection(ConfigurationManager.ParametersConnectionStrings["mycon"].AddWithValueToString("@ID", objuser.ID));

                SqlCommand cmd.Parameters.AddWithValue = new SqlCommand("@Nome""Usp_InserirAtualizarDeletar_Usuario", objuser.Nomecon)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@Sobrenome""@ID", objuser.SobrenomeID);

                cmd.Parameters.AddWithValue("@Nascimento""@Nome", objuser.NascimentoNome);

                cmd.Parameters.AddWithValue("@Query""@Sobrenome", 2objuser.Sobrenome);

            con    cmd.OpenParameters.AddWithValue("@Nascimento", objuser.Nascimento);

            result =   cmd.ExecuteScalar()Parameters.ToStringAddWithValue("@Query", 2);

            return result;   con.Open();

        }        result = cmd.ExecuteScalar().ToString();

        catch        return result;

        {    }

            return result = "";catch

        }    {

        finally        return result = "";

        {    }

            con.Close();finally

        }    {

    }            con.Close();

    public string DeletarDados(Usuario objuser)     }

    {    }

        SqlConnection con =public newstring SqlConnectionDeletarDados("LocalDB)\\MSSQLLocalDB"Usuario objuser);

        string result = "";{

        try    SqlConnection con = new SqlConnection("LocalDB)\\MSSQLLocalDB");

        {    string result = "";

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());try

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd    con = new SqlConnection(ConfigurationManager.ParametersConnectionStrings["mycon"].AddWithValueToString("@ID", objuser.ID));

                SqlCommand cmd.Parameters.AddWithValue = new SqlCommand("@Nome""Usp_InserirAtualizarDeletar_Usuario", nullcon)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@Sobrenome""@ID", nullobjuser.ID);

                cmd.Parameters.AddWithValue("@Nascimento""@Nome", null);

                cmd.Parameters.AddWithValue("@Query""@Sobrenome", 3null);

            con    cmd.OpenParameters.AddWithValue("@Nascimento", null);

            result =   cmd.ExecuteScalar()Parameters.ToStringAddWithValue("@Query", 3);

            return result;   con.Open();

        }        result = cmd.ExecuteScalar().ToString();

        catch        return result;

        {    }

            return result = "";catch

        }    {

        finally        return result = "";

        {    }

            con.Close();finally

        }    {

    }            con.Close();

    public List<Usuario> SelecionarTodosDados()      }

    {    }

        SqlConnection conpublic =List<Usuario> null;SelecionarTodosDados()

        DataSet ds = null;{

        List<Usuario> userlist   SqlConnection con = null;

        try    DataSet ds = null;

        {    List<Usuario> userlist = null;

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());try

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd    con = new SqlConnection(ConfigurationManager.ParametersConnectionStrings["mycon"].AddWithValueToString("@ID", null));

                SqlCommand cmd.Parameters.AddWithValue = new SqlCommand("@Nome""Usp_InserirAtualizarDeletar_Usuario", nullcon)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@Sobrenome""@ID", null);

                cmd.Parameters.AddWithValue("@Nascimento""@Nome", null);

                cmd.Parameters.AddWithValue("@Query""@Sobrenome", 4null);

            con    cmd.OpenParameters.AddWithValue("@Nascimento", null);

            SqlDataAdapter da = new SqlDataAdapter
            {
                SelectCommand = cmd
           .Parameters.AddWithValue("@Query", }4);

            ds = new DataSet con.Open();

                SqlDataAdapter da.Fill(ds) = new SqlDataAdapter
                {
                    SelectCommand = cmd
                };

            userlist    ds = new List<Usuario>DataSet();

            for (int i = 0; i < ds.Tables[0].Rowsda.Count; i++Fill(ds);

            {
                Usuario cobjuserlist = new Usuario
                {
                    ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToStringList<Usuario>()),;

                for (int i = Nome0; =i < ds.Tables[0].Rows[i]["Nome"]Rows.ToString(Count; i++),

                {
    Sobrenome                Usuario cobj = new Usuario
                    {
                        ID = Convert.ToInt32(ds.Tables[0].Rows[i]["Sobrenome"]Rows[i]["ID"].ToString()),

                    Nascimento    Nome = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"]Rows[i]["Nome"].ToString())
                };,

                        Sobrenome = ds.Tables[0].Rows[i]["Sobrenome"].ToString(),

                        Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                    };

                userlist.Add(cobj);

            }

            return userlist;       userlist.Add(cobj);

                }

        catch        return userlist;

        {    }

            return userlist;catch

        }    {

        finally        return userlist;

        {    }

            con.Close();finally

        }    {

    }            con.Close();

    public Usuario SelecionarDadosbyID(string ID)     }

    {    }

        SqlConnectionpublic conUsuario =SelecionarDadosbyID(string null;ID)

        DataSet ds = null;{

        Usuario cobj   SqlConnection con = null;

        try    DataSet ds = null;

        {    Usuario cobj = null;

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());try

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd    con = new SqlConnection(ConfigurationManager.ParametersConnectionStrings["mycon"].AddWithValueToString("@ID", ID));

                SqlCommand cmd.Parameters.AddWithValue = new SqlCommand("@Nome""Usp_InserirAtualizarDeletar_Usuario", nullcon)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@Sobrenome""@ID", nullID);

                cmd.Parameters.AddWithValue("@Nascimento""@Nome", null);

                cmd.Parameters.AddWithValue("@Query""@Sobrenome", 5null);

            SqlDataAdapter da = new SqlDataAdapter
            {
                SelectCommand = cmd
           .Parameters.AddWithValue("@Nascimento", }null);

            ds = new DataSet cmd.Parameters.AddWithValue("@Query", 5);

                SqlDataAdapter da.Fill(ds) = new SqlDataAdapter
                {
                    SelectCommand = cmd
                };

                ds = new DataSet();

                da.Fill(ds);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {

                cobjfor (int i = new0; Usuarioi < ds.Tables[0].Rows.Count; i++)

                {
                    ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString()),

                    Nomecobj = new Usuario
                    {
                        ID = Convert.ToInt32(ds.Tables[0].Rows[i]["Nome"]Rows[i]["ID"].ToString()),

                    Sobrenome    Nome = ds.Tables[0].Rows[i]["Sobrenome"]Rows[i]["Nome"].ToString(),

                    Nascimento    Sobrenome = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"]Rows[i]["Sobrenome"].ToString())
                };,

                        Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                    };

            return cobj;   }

        }        return cobj;

        catch    }

        {    catch

            return cobj;{

        }        return cobj;

        finally    }

        {    finally

            con.Close();{

                con.Close();

            }

        }

    }

 }

}insira o código aqui

using System;

using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web;

namespace Roger_Silva_DR2_AT.DAL { public class DataAccessLayer {

    public string InserirDados(Usuario objuser)

    {
        SqlConnection con = null;

        string result = "";

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtulizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", 0);

            cmd.Parameters.AddWithValue("@Nome", objuser.Nome);

            cmd.Parameters.AddWithValue("@Sobrenome", objuser.Sobrenome);

            cmd.Parameters.AddWithValue("@Nascimento", objuser.Nascimento);

            cmd.Parameters.AddWithValue("@Query", 1);

            con.Open();

            result = cmd.ExecuteScalar().ToString();

            return result;

        }

        catch

        {

            return result = "";

        }

        finally

        {

            con.Close();

        }

    }

    public string AtualizarDados(Usuario objuser)

    {

        SqlConnection con = null;

        string result = "";

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", objuser.ID);

            cmd.Parameters.AddWithValue("@Nome", objuser.Nome);

            cmd.Parameters.AddWithValue("@Sobrenome", objuser.Sobrenome);

            cmd.Parameters.AddWithValue("@Nascimento", objuser.Nascimento);

            cmd.Parameters.AddWithValue("@Query", 2);

            con.Open();

            result = cmd.ExecuteScalar().ToString();

            return result;

        }

        catch

        {

            return result = "";

        }

        finally

        {

            con.Close();

        }

    }

    public string DeletarDados(Usuario objuser)

    {

        SqlConnection con = new SqlConnection("LocalDB)\\MSSQLLocalDB");

        string result = "";

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", objuser.ID);

            cmd.Parameters.AddWithValue("@Nome", null);

            cmd.Parameters.AddWithValue("@Sobrenome", null);

            cmd.Parameters.AddWithValue("@Nascimento", null);

            cmd.Parameters.AddWithValue("@Query", 3);

            con.Open();

            result = cmd.ExecuteScalar().ToString();

            return result;

        }

        catch

        {

            return result = "";

        }

        finally

        {

            con.Close();

        }

    }

    public List<Usuario> SelecionarTodosDados()

    {

        SqlConnection con = null;

        DataSet ds = null;

        List<Usuario> userlist = null;

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", null);

            cmd.Parameters.AddWithValue("@Nome", null);

            cmd.Parameters.AddWithValue("@Sobrenome", null);

            cmd.Parameters.AddWithValue("@Nascimento", null);

            cmd.Parameters.AddWithValue("@Query", 4);

            con.Open();

            SqlDataAdapter da = new SqlDataAdapter
            {
                SelectCommand = cmd
            };

            ds = new DataSet();

            da.Fill(ds);

            userlist = new List<Usuario>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {
                Usuario cobj = new Usuario
                {
                    ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString()),

                    Nome = ds.Tables[0].Rows[i]["Nome"].ToString(),

                    Sobrenome = ds.Tables[0].Rows[i]["Sobrenome"].ToString(),

                    Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                };



                userlist.Add(cobj);

            }

            return userlist;

        }

        catch

        {

            return userlist;

        }

        finally

        {

            con.Close();

        }

    }

    public Usuario SelecionarDadosbyID(string ID)

    {

        SqlConnection con = null;

        DataSet ds = null;

        Usuario cobj = null;

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", ID);

            cmd.Parameters.AddWithValue("@Nome", null);

            cmd.Parameters.AddWithValue("@Sobrenome", null);

            cmd.Parameters.AddWithValue("@Nascimento", null);

            cmd.Parameters.AddWithValue("@Query", 5);

            SqlDataAdapter da = new SqlDataAdapter
            {
                SelectCommand = cmd
            };

            ds = new DataSet();

            da.Fill(ds);



            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {

                cobj = new Usuario
                {
                    ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString()),

                    Nome = ds.Tables[0].Rows[i]["Nome"].ToString(),

                    Sobrenome = ds.Tables[0].Rows[i]["Sobrenome"].ToString(),

                    Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                };

            }

            return cobj;

        }

        catch

        {

            return cobj;

        }

        finally

        {

            con.Close();

        }

    }

 }

}insira o código aqui

    using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Roger_Silva_DR2_AT.DAL
{
    public class DataAccessLayer
    {

        public string InserirDados(Usuario objuser)

        {
            SqlConnection con = null;

            string result = "";

            try

            {

                con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

                SqlCommand cmd = new SqlCommand("Usp_InserirAtulizarDeletar_Usuario", con)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@ID", 0);

                cmd.Parameters.AddWithValue("@Nome", objuser.Nome);

                cmd.Parameters.AddWithValue("@Sobrenome", objuser.Sobrenome);

                cmd.Parameters.AddWithValue("@Nascimento", objuser.Nascimento);

                cmd.Parameters.AddWithValue("@Query", 1);

                con.Open();

                result = cmd.ExecuteScalar().ToString();

                return result;

            }

            catch

            {

                return result = "";

            }

            finally

            {

                con.Close();

            }

        }

        public string AtualizarDados(Usuario objuser)

        {

            SqlConnection con = null;

            string result = "";

            try

            {

                con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

                SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@ID", objuser.ID);

                cmd.Parameters.AddWithValue("@Nome", objuser.Nome);

                cmd.Parameters.AddWithValue("@Sobrenome", objuser.Sobrenome);

                cmd.Parameters.AddWithValue("@Nascimento", objuser.Nascimento);

                cmd.Parameters.AddWithValue("@Query", 2);

                con.Open();

                result = cmd.ExecuteScalar().ToString();

                return result;

            }

            catch

            {

                return result = "";

            }

            finally

            {

                con.Close();

            }

        }

        public string DeletarDados(Usuario objuser)

        {

            SqlConnection con = new SqlConnection("LocalDB)\\MSSQLLocalDB");

            string result = "";

            try

            {

                con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

                SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@ID", objuser.ID);

                cmd.Parameters.AddWithValue("@Nome", null);

                cmd.Parameters.AddWithValue("@Sobrenome", null);

                cmd.Parameters.AddWithValue("@Nascimento", null);

                cmd.Parameters.AddWithValue("@Query", 3);

                con.Open();

                result = cmd.ExecuteScalar().ToString();

                return result;

            }

            catch

            {

                return result = "";

            }

            finally

            {

                con.Close();

            }

        }

        public List<Usuario> SelecionarTodosDados()

        {

            SqlConnection con = null;

            DataSet ds = null;

            List<Usuario> userlist = null;

            try

            {

                con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

                SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@ID", null);

                cmd.Parameters.AddWithValue("@Nome", null);

                cmd.Parameters.AddWithValue("@Sobrenome", null);

                cmd.Parameters.AddWithValue("@Nascimento", null);

                cmd.Parameters.AddWithValue("@Query", 4);

                con.Open();

                SqlDataAdapter da = new SqlDataAdapter
                {
                    SelectCommand = cmd
                };

                ds = new DataSet();

                da.Fill(ds);

                userlist = new List<Usuario>();

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                {
                    Usuario cobj = new Usuario
                    {
                        ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString()),

                        Nome = ds.Tables[0].Rows[i]["Nome"].ToString(),

                        Sobrenome = ds.Tables[0].Rows[i]["Sobrenome"].ToString(),

                        Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                    };



                    userlist.Add(cobj);

                }

                return userlist;

            }

            catch

            {

                return userlist;

            }

            finally

            {

                con.Close();

            }

        }

        public Usuario SelecionarDadosbyID(string ID)

        {

            SqlConnection con = null;

            DataSet ds = null;

            Usuario cobj = null;

            try

            {

                con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

                SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
                {
                    CommandType = CommandType.StoredProcedure
                };

                cmd.Parameters.AddWithValue("@ID", ID);

                cmd.Parameters.AddWithValue("@Nome", null);

                cmd.Parameters.AddWithValue("@Sobrenome", null);

                cmd.Parameters.AddWithValue("@Nascimento", null);

                cmd.Parameters.AddWithValue("@Query", 5);

                SqlDataAdapter da = new SqlDataAdapter
                {
                    SelectCommand = cmd
                };

                ds = new DataSet();

                da.Fill(ds);



                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                {

                    cobj = new Usuario
                    {
                        ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString()),

                        Nome = ds.Tables[0].Rows[i]["Nome"].ToString(),

                        Sobrenome = ds.Tables[0].Rows[i]["Sobrenome"].ToString(),

                        Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                    };

                }

                return cobj;

            }

            catch

            {

                return cobj;

            }

            finally

            {

                con.Close();

            }

        }

    }
}
Fonte Link

System.NullReferenceException: 'Referência de objeto não definida para uma instância de um objeto

estava tentando rodar meu banco de dados mas aparece esse erro. System.NullReferenceException: 'Referência de objeto não definida para uma instância de um objeto.

con era null

using System;

using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web;

namespace Roger_Silva_DR2_AT.DAL { public class DataAccessLayer {

    public string InserirDados(Usuario objuser)

    {
        SqlConnection con = null;

        string result = "";

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtulizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", 0);

            cmd.Parameters.AddWithValue("@Nome", objuser.Nome);

            cmd.Parameters.AddWithValue("@Sobrenome", objuser.Sobrenome);

            cmd.Parameters.AddWithValue("@Nascimento", objuser.Nascimento);

            cmd.Parameters.AddWithValue("@Query", 1);

            con.Open();

            result = cmd.ExecuteScalar().ToString();

            return result;

        }

        catch

        {

            return result = "";

        }

        finally

        {

            con.Close();

        }

    }

    public string AtualizarDados(Usuario objuser)

    {

        SqlConnection con = null;

        string result = "";

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", objuser.ID);

            cmd.Parameters.AddWithValue("@Nome", objuser.Nome);

            cmd.Parameters.AddWithValue("@Sobrenome", objuser.Sobrenome);

            cmd.Parameters.AddWithValue("@Nascimento", objuser.Nascimento);

            cmd.Parameters.AddWithValue("@Query", 2);

            con.Open();

            result = cmd.ExecuteScalar().ToString();

            return result;

        }

        catch

        {

            return result = "";

        }

        finally

        {

            con.Close();

        }

    }

    public string DeletarDados(Usuario objuser)

    {

        SqlConnection con = new SqlConnection("LocalDB)\\MSSQLLocalDB");

        string result = "";

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", objuser.ID);

            cmd.Parameters.AddWithValue("@Nome", null);

            cmd.Parameters.AddWithValue("@Sobrenome", null);

            cmd.Parameters.AddWithValue("@Nascimento", null);

            cmd.Parameters.AddWithValue("@Query", 3);

            con.Open();

            result = cmd.ExecuteScalar().ToString();

            return result;

        }

        catch

        {

            return result = "";

        }

        finally

        {

            con.Close();

        }

    }

    public List<Usuario> SelecionarTodosDados()

    {

        SqlConnection con = null;

        DataSet ds = null;

        List<Usuario> userlist = null;

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", null);

            cmd.Parameters.AddWithValue("@Nome", null);

            cmd.Parameters.AddWithValue("@Sobrenome", null);

            cmd.Parameters.AddWithValue("@Nascimento", null);

            cmd.Parameters.AddWithValue("@Query", 4);

            con.Open();

            SqlDataAdapter da = new SqlDataAdapter
            {
                SelectCommand = cmd
            };

            ds = new DataSet();

            da.Fill(ds);

            userlist = new List<Usuario>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {
                Usuario cobj = new Usuario
                {
                    ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString()),

                    Nome = ds.Tables[0].Rows[i]["Nome"].ToString(),

                    Sobrenome = ds.Tables[0].Rows[i]["Sobrenome"].ToString(),

                    Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                };



                userlist.Add(cobj);

            }

            return userlist;

        }

        catch

        {

            return userlist;

        }

        finally

        {

            con.Close();

        }

    }

    public Usuario SelecionarDadosbyID(string ID)

    {

        SqlConnection con = null;

        DataSet ds = null;

        Usuario cobj = null;

        try

        {

            con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());

            SqlCommand cmd = new SqlCommand("Usp_InserirAtualizarDeletar_Usuario", con)
            {
                CommandType = CommandType.StoredProcedure
            };

            cmd.Parameters.AddWithValue("@ID", ID);

            cmd.Parameters.AddWithValue("@Nome", null);

            cmd.Parameters.AddWithValue("@Sobrenome", null);

            cmd.Parameters.AddWithValue("@Nascimento", null);

            cmd.Parameters.AddWithValue("@Query", 5);

            SqlDataAdapter da = new SqlDataAdapter
            {
                SelectCommand = cmd
            };

            ds = new DataSet();

            da.Fill(ds);



            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

            {

                cobj = new Usuario
                {
                    ID = Convert.ToInt32(ds.Tables[0].Rows[i]["ID"].ToString()),

                    Nome = ds.Tables[0].Rows[i]["Nome"].ToString(),

                    Sobrenome = ds.Tables[0].Rows[i]["Sobrenome"].ToString(),

                    Nascimento = Convert.ToDateTime(ds.Tables[0].Rows[i]["Nascimento"].ToString())
                };

            }

            return cobj;

        }

        catch

        {

            return cobj;

        }

        finally

        {

            con.Close();

        }

    }

}

}insira o código aqui