0

Using the code from Call a stored procedure with parameter in c# this article.

This is my code:

using (SqlConnection con = new SqlConnection(dc.Con))
{
    using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userName;
        cmd.Parameters.Add("@ScrambledPass", SqlDbType.VarChar).Value = encryptedPass;

        con.Open();
        cmd.ExecuteNonQuery();
    }
}

dc in the dc.Con section "does not exist in the current context" and shows an error. How do I fix this? Are there any dependencies or prerequisites I need? I already have using System.Data.SqlClient; System.Data and other basic essentials for this.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Poyo
  • 1
  • 3
    And what is _dc_ ? In the copied question dc is some kind of global variable that has a string property (_con_) that contains the [connectionstring](https://www.connectionstrings.com/connection-strings-explained/?msclkid=b53d42ccb6ad11ec86a4031dc5af067f). (IE. The information needed to identify and reach your database). Of course, if you don't have this variable properly initialized this code cannot work. – Steve Apr 07 '22 at 20:01
  • 3
    FYI the prefix `sp_` is reserved, by Microsoft, for **S**pecial / **S**ystem **P**rocedures. It should *not* be used for User Procedures. Doing so comes with a performance cost and the risk of your Procedure simply not working one day after an update/upgrade. Either use a different prefix or (possibly better) no prefix at all. [Is the sp_ prefix still a no-no?](https://sqlperformance.com/2012/10/t-sql-queries/sp_prefix) – Thom A Apr 07 '22 at 20:06
  • 1
    *any ... prerequisites I need?* - reading [the fine manual](https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.-ctor?view=dotnet-plat-ext-6.0#system-data-sqlclient-sqlconnection-ctor(system-string)) is probably a reasonable prerequisite; possibly faster than asking a question on SO too.. – Caius Jard Apr 07 '22 at 20:20
  • Wrap your code in a try catch and look into error. – duerzd696 Apr 07 '22 at 20:33

0 Answers0