Object reference not set in [duplicate]












-2
















This question already has an answer here:




  • What is a NullReferenceException, and how do I fix it?

    31 answers




public static Boolean ValidateUser(string struser, string strpass)
{
// Establish connection information
SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"].ConnectionString);
// Establish SQL command
SqlCommand sql_comm = new SqlCommand("SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn_string);
// Provide Parameter values
sql_comm.Parameters.AddWithValue("@usuario", struser);
sql_comm.Parameters.AddWithValue("@contrasena", strpass);
// Open the connection
conn_string.Open();
// Execute the SQL command and assign the resulting value to an integer variable
Int32 intUserCount = Convert.ToInt32(sql_comm.ExecuteScalar());
// Close the connection
conn_string.Close();
// Evaluate the integer variable content; greater than cero is a valid combination
if (intUserCount == 1)
{
return true;
}
else
{
return false;
}
}


can somebody tell me why this error is prompted?



enter image description here










share|improve this question















marked as duplicate by Dour High Arch, Lasse Vågsæther Karlsen c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    You have to put the name of the connection string in the braces, not the connection string itself. Or you use SqlConnection conn_string = new SqlConnection("Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******""); Replace the wildcards with the password of course

    – schlonzo
    Nov 20 '18 at 19:01








  • 1





    Read What is a NullReferenceException, learn how to debug, and stop showing pictures of errors.

    – Dour High Arch
    Nov 20 '18 at 19:03











  • I believe @schlonzo answered correctly. The rest of your code looks ok. I would use a using(){} instead though. Also, it's probably best not to include any real User IDs, or IP Addresses.

    – FernandoG
    Nov 20 '18 at 19:06
















-2
















This question already has an answer here:




  • What is a NullReferenceException, and how do I fix it?

    31 answers




public static Boolean ValidateUser(string struser, string strpass)
{
// Establish connection information
SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"].ConnectionString);
// Establish SQL command
SqlCommand sql_comm = new SqlCommand("SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn_string);
// Provide Parameter values
sql_comm.Parameters.AddWithValue("@usuario", struser);
sql_comm.Parameters.AddWithValue("@contrasena", strpass);
// Open the connection
conn_string.Open();
// Execute the SQL command and assign the resulting value to an integer variable
Int32 intUserCount = Convert.ToInt32(sql_comm.ExecuteScalar());
// Close the connection
conn_string.Close();
// Evaluate the integer variable content; greater than cero is a valid combination
if (intUserCount == 1)
{
return true;
}
else
{
return false;
}
}


can somebody tell me why this error is prompted?



enter image description here










share|improve this question















marked as duplicate by Dour High Arch, Lasse Vågsæther Karlsen c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    You have to put the name of the connection string in the braces, not the connection string itself. Or you use SqlConnection conn_string = new SqlConnection("Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******""); Replace the wildcards with the password of course

    – schlonzo
    Nov 20 '18 at 19:01








  • 1





    Read What is a NullReferenceException, learn how to debug, and stop showing pictures of errors.

    – Dour High Arch
    Nov 20 '18 at 19:03











  • I believe @schlonzo answered correctly. The rest of your code looks ok. I would use a using(){} instead though. Also, it's probably best not to include any real User IDs, or IP Addresses.

    – FernandoG
    Nov 20 '18 at 19:06














-2












-2








-2









This question already has an answer here:




  • What is a NullReferenceException, and how do I fix it?

    31 answers




public static Boolean ValidateUser(string struser, string strpass)
{
// Establish connection information
SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"].ConnectionString);
// Establish SQL command
SqlCommand sql_comm = new SqlCommand("SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn_string);
// Provide Parameter values
sql_comm.Parameters.AddWithValue("@usuario", struser);
sql_comm.Parameters.AddWithValue("@contrasena", strpass);
// Open the connection
conn_string.Open();
// Execute the SQL command and assign the resulting value to an integer variable
Int32 intUserCount = Convert.ToInt32(sql_comm.ExecuteScalar());
// Close the connection
conn_string.Close();
// Evaluate the integer variable content; greater than cero is a valid combination
if (intUserCount == 1)
{
return true;
}
else
{
return false;
}
}


can somebody tell me why this error is prompted?



enter image description here










share|improve this question

















This question already has an answer here:




  • What is a NullReferenceException, and how do I fix it?

    31 answers




public static Boolean ValidateUser(string struser, string strpass)
{
// Establish connection information
SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"].ConnectionString);
// Establish SQL command
SqlCommand sql_comm = new SqlCommand("SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn_string);
// Provide Parameter values
sql_comm.Parameters.AddWithValue("@usuario", struser);
sql_comm.Parameters.AddWithValue("@contrasena", strpass);
// Open the connection
conn_string.Open();
// Execute the SQL command and assign the resulting value to an integer variable
Int32 intUserCount = Convert.ToInt32(sql_comm.ExecuteScalar());
// Close the connection
conn_string.Close();
// Evaluate the integer variable content; greater than cero is a valid combination
if (intUserCount == 1)
{
return true;
}
else
{
return false;
}
}


can somebody tell me why this error is prompted?



enter image description here





This question already has an answer here:




  • What is a NullReferenceException, and how do I fix it?

    31 answers








c# asp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 18:57









Joel Coehoorn

309k95494728




309k95494728










asked Nov 20 '18 at 18:51









Hiram MelendezHiram Melendez

1




1




marked as duplicate by Dour High Arch, Lasse Vågsæther Karlsen c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Dour High Arch, Lasse Vågsæther Karlsen c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 20:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    You have to put the name of the connection string in the braces, not the connection string itself. Or you use SqlConnection conn_string = new SqlConnection("Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******""); Replace the wildcards with the password of course

    – schlonzo
    Nov 20 '18 at 19:01








  • 1





    Read What is a NullReferenceException, learn how to debug, and stop showing pictures of errors.

    – Dour High Arch
    Nov 20 '18 at 19:03











  • I believe @schlonzo answered correctly. The rest of your code looks ok. I would use a using(){} instead though. Also, it's probably best not to include any real User IDs, or IP Addresses.

    – FernandoG
    Nov 20 '18 at 19:06














  • 2





    You have to put the name of the connection string in the braces, not the connection string itself. Or you use SqlConnection conn_string = new SqlConnection("Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******""); Replace the wildcards with the password of course

    – schlonzo
    Nov 20 '18 at 19:01








  • 1





    Read What is a NullReferenceException, learn how to debug, and stop showing pictures of errors.

    – Dour High Arch
    Nov 20 '18 at 19:03











  • I believe @schlonzo answered correctly. The rest of your code looks ok. I would use a using(){} instead though. Also, it's probably best not to include any real User IDs, or IP Addresses.

    – FernandoG
    Nov 20 '18 at 19:06








2




2





You have to put the name of the connection string in the braces, not the connection string itself. Or you use SqlConnection conn_string = new SqlConnection("Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******""); Replace the wildcards with the password of course

– schlonzo
Nov 20 '18 at 19:01







You have to put the name of the connection string in the braces, not the connection string itself. Or you use SqlConnection conn_string = new SqlConnection("Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******""); Replace the wildcards with the password of course

– schlonzo
Nov 20 '18 at 19:01






1




1





Read What is a NullReferenceException, learn how to debug, and stop showing pictures of errors.

– Dour High Arch
Nov 20 '18 at 19:03





Read What is a NullReferenceException, learn how to debug, and stop showing pictures of errors.

– Dour High Arch
Nov 20 '18 at 19:03













I believe @schlonzo answered correctly. The rest of your code looks ok. I would use a using(){} instead though. Also, it's probably best not to include any real User IDs, or IP Addresses.

– FernandoG
Nov 20 '18 at 19:06





I believe @schlonzo answered correctly. The rest of your code looks ok. I would use a using(){} instead though. Also, it's probably best not to include any real User IDs, or IP Addresses.

– FernandoG
Nov 20 '18 at 19:06












2 Answers
2






active

oldest

votes


















1














This expression from the line highlighted in red in the exception results is null:



System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"]


It's null because the Connection property expects the name of a string, and not the full string. It's trying lookup the string in a collection, doesn't find that big long blob of text, and so returns null.



Given the above, you then try to the reference the .Connection string property of a null reference. It's as if you had done this:



null.ConnectionString;


Either change that code to use the name of the connection string as listed in the web.config file, or since you have the whole string there already just give that string directly to the SqlConnection() constructor. Either way, that code should be cleaned up some:



//comments should focus on "why", rather than "what"
public static Boolean ValidateUser(string struser, string strpass)
{
using (var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn string name"].ConnectionString))
using (var sql_comm = new SqlCommand(""SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn))
{
//Don't use AddWithValue(). It forces ADO.Net to guess about parameter types.
//Use exact column types and lengths instead
sql_comm.Parameters.Add("@usuario", SqlDbType.NVarChar, 50).Value = struser;
//Dear God, please tell me you're not using a plain-text password? That's a HUGE mistake!
sql_comm.Parameters.Add("@contrasena", SqlDbType.NVarChar, 180).Value = strpass;

conn.Open();
return (1 == (int)sql_comm.ExecuteScalar());
}
}





share|improve this answer

































    0














    If you have this in your web.config, it means your connection string name is "Default"



    <connectionStrings>
    <add name="Default" connectionString="Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******" providerName="System.Data.SqlClient" />
    <connectionStrings>


    So, the correct way to add connection string in your code is-



    SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Default"].ConnectionString);





    share|improve this answer






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      This expression from the line highlighted in red in the exception results is null:



      System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"]


      It's null because the Connection property expects the name of a string, and not the full string. It's trying lookup the string in a collection, doesn't find that big long blob of text, and so returns null.



      Given the above, you then try to the reference the .Connection string property of a null reference. It's as if you had done this:



      null.ConnectionString;


      Either change that code to use the name of the connection string as listed in the web.config file, or since you have the whole string there already just give that string directly to the SqlConnection() constructor. Either way, that code should be cleaned up some:



      //comments should focus on "why", rather than "what"
      public static Boolean ValidateUser(string struser, string strpass)
      {
      using (var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn string name"].ConnectionString))
      using (var sql_comm = new SqlCommand(""SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn))
      {
      //Don't use AddWithValue(). It forces ADO.Net to guess about parameter types.
      //Use exact column types and lengths instead
      sql_comm.Parameters.Add("@usuario", SqlDbType.NVarChar, 50).Value = struser;
      //Dear God, please tell me you're not using a plain-text password? That's a HUGE mistake!
      sql_comm.Parameters.Add("@contrasena", SqlDbType.NVarChar, 180).Value = strpass;

      conn.Open();
      return (1 == (int)sql_comm.ExecuteScalar());
      }
      }





      share|improve this answer






























        1














        This expression from the line highlighted in red in the exception results is null:



        System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"]


        It's null because the Connection property expects the name of a string, and not the full string. It's trying lookup the string in a collection, doesn't find that big long blob of text, and so returns null.



        Given the above, you then try to the reference the .Connection string property of a null reference. It's as if you had done this:



        null.ConnectionString;


        Either change that code to use the name of the connection string as listed in the web.config file, or since you have the whole string there already just give that string directly to the SqlConnection() constructor. Either way, that code should be cleaned up some:



        //comments should focus on "why", rather than "what"
        public static Boolean ValidateUser(string struser, string strpass)
        {
        using (var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn string name"].ConnectionString))
        using (var sql_comm = new SqlCommand(""SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn))
        {
        //Don't use AddWithValue(). It forces ADO.Net to guess about parameter types.
        //Use exact column types and lengths instead
        sql_comm.Parameters.Add("@usuario", SqlDbType.NVarChar, 50).Value = struser;
        //Dear God, please tell me you're not using a plain-text password? That's a HUGE mistake!
        sql_comm.Parameters.Add("@contrasena", SqlDbType.NVarChar, 180).Value = strpass;

        conn.Open();
        return (1 == (int)sql_comm.ExecuteScalar());
        }
        }





        share|improve this answer




























          1












          1








          1







          This expression from the line highlighted in red in the exception results is null:



          System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"]


          It's null because the Connection property expects the name of a string, and not the full string. It's trying lookup the string in a collection, doesn't find that big long blob of text, and so returns null.



          Given the above, you then try to the reference the .Connection string property of a null reference. It's as if you had done this:



          null.ConnectionString;


          Either change that code to use the name of the connection string as listed in the web.config file, or since you have the whole string there already just give that string directly to the SqlConnection() constructor. Either way, that code should be cleaned up some:



          //comments should focus on "why", rather than "what"
          public static Boolean ValidateUser(string struser, string strpass)
          {
          using (var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn string name"].ConnectionString))
          using (var sql_comm = new SqlCommand(""SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn))
          {
          //Don't use AddWithValue(). It forces ADO.Net to guess about parameter types.
          //Use exact column types and lengths instead
          sql_comm.Parameters.Add("@usuario", SqlDbType.NVarChar, 50).Value = struser;
          //Dear God, please tell me you're not using a plain-text password? That's a HUGE mistake!
          sql_comm.Parameters.Add("@contrasena", SqlDbType.NVarChar, 180).Value = strpass;

          conn.Open();
          return (1 == (int)sql_comm.ExecuteScalar());
          }
          }





          share|improve this answer















          This expression from the line highlighted in red in the exception results is null:



          System.Configuration.ConfigurationManager.ConnectionStrings["Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******"]


          It's null because the Connection property expects the name of a string, and not the full string. It's trying lookup the string in a collection, doesn't find that big long blob of text, and so returns null.



          Given the above, you then try to the reference the .Connection string property of a null reference. It's as if you had done this:



          null.ConnectionString;


          Either change that code to use the name of the connection string as listed in the web.config file, or since you have the whole string there already just give that string directly to the SqlConnection() constructor. Either way, that code should be cleaned up some:



          //comments should focus on "why", rather than "what"
          public static Boolean ValidateUser(string struser, string strpass)
          {
          using (var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conn string name"].ConnectionString))
          using (var sql_comm = new SqlCommand(""SELECT count(userID) FROM HIEPA.HIEPA_USERS where UserName = @usuario and UserPassword = @contrasena ; ", conn))
          {
          //Don't use AddWithValue(). It forces ADO.Net to guess about parameter types.
          //Use exact column types and lengths instead
          sql_comm.Parameters.Add("@usuario", SqlDbType.NVarChar, 50).Value = struser;
          //Dear God, please tell me you're not using a plain-text password? That's a HUGE mistake!
          sql_comm.Parameters.Add("@contrasena", SqlDbType.NVarChar, 180).Value = strpass;

          conn.Open();
          return (1 == (int)sql_comm.ExecuteScalar());
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 20:31

























          answered Nov 20 '18 at 19:06









          Joel CoehoornJoel Coehoorn

          309k95494728




          309k95494728

























              0














              If you have this in your web.config, it means your connection string name is "Default"



              <connectionStrings>
              <add name="Default" connectionString="Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******" providerName="System.Data.SqlClient" />
              <connectionStrings>


              So, the correct way to add connection string in your code is-



              SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Default"].ConnectionString);





              share|improve this answer




























                0














                If you have this in your web.config, it means your connection string name is "Default"



                <connectionStrings>
                <add name="Default" connectionString="Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******" providerName="System.Data.SqlClient" />
                <connectionStrings>


                So, the correct way to add connection string in your code is-



                SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Default"].ConnectionString);





                share|improve this answer


























                  0












                  0








                  0







                  If you have this in your web.config, it means your connection string name is "Default"



                  <connectionStrings>
                  <add name="Default" connectionString="Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******" providerName="System.Data.SqlClient" />
                  <connectionStrings>


                  So, the correct way to add connection string in your code is-



                  SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Default"].ConnectionString);





                  share|improve this answer













                  If you have this in your web.config, it means your connection string name is "Default"



                  <connectionStrings>
                  <add name="Default" connectionString="Data Source=137.145.169.3;Initial Catalog=SICI4266;Persist Security Info=True;User ID=SICI4266G3;Password=*******" providerName="System.Data.SqlClient" />
                  <connectionStrings>


                  So, the correct way to add connection string in your code is-



                  SqlConnection conn_string = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Default"].ConnectionString);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 19:12









                  kamalpreetkamalpreet

                  1,1811433




                  1,1811433















                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?