1

In a button click event I have this

protected void btnPassword_Click(object sender, EventArgs e) 
{ 
    string UserID = "Test"; 
    string password = txtPassword.Text; 
    var random = new Random(); 
    Salt = GenerateSalt(); 
    Iterations = random.Next(10000, 100000); 
    Hash = GenerateHash(password, Salt, (int)Iterations); 
    Response.Write(Hash + "<br />" + Salt + "<br />" + Iterations); 
} 

and I am able to see the results.

Why is it that I get an object reference error when trying to put it into a database:

protected void btnPassword_Click(object sender, EventArgs e) 
{ 
    string UserID = "Test"; 
    string password = txtPassword.Text; 
    var random = new Random(); 
    Salt = GenerateSalt(); 
    Iterations = random.Next(10000, 100000); 
    Hash = GenerateHash(password, Salt, (int)Iterations); 

    // The error occurs on the following line
    using (SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer2"].ConnectionString)) 
    { 
        SqlCommand cmd1 = new SqlCommand(@"INSERT INTO [MyTable](UserID,
                                           Hash, Salt, Iterations) 
                VALUES (@UserID, @Hash, @Salt, @Iterations)", conn2); 
        cmd1.CommandType = CommandType.Text; 
        conn2.Open(); 
        cmd1.Parameters.AddWithValue("@UserId", UserID); 
        cmd1.Parameters.AddWithValue("@Hash", Hash); 
        cmd1.Parameters.AddWithValue("@Salt", Salt); 
        cmd1.Parameters.AddWithValue("@Iterations",Convert.ToInt32(Iterations)); 
        cmd1.ExecuteNonQuery(); 
    } 
}

Stack trace:

[NullReferenceException: Object reference not set to an instance of an object.] ashlandapp_bootstrap.Encrypt.btnPassword_Click(Object sender, EventArgs e) in c:\Users\20002143\Documents\Visual Studio 2012\Projects\ashlandapp-bootstrap\ashlandapp-bootstrap\Encrypt.aspx.cs:66 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9556538 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Skullomania
  • 2,225
  • 2
  • 29
  • 65
  • 3
    Well *where* are you getting the error? Please give us a stack trace... (It's not clear why you've got `Salt`, `Iterations` and `Hash` as properties here rather than local variables, by the way.) – Jon Skeet Feb 06 '15 at 16:37
  • I am getting the error in the using Sql line – Skullomania Feb 06 '15 at 16:39
  • 8
    Because you don't have connection string "SiteSqlServer2" in the correct place in web.config. – Igor Feb 06 '15 at 16:39
  • you really don't need the `@` in this query `SqlCommand cmd1 = new SqlCommand(@"` also is this key in the .config file `"SiteSqlServer2"` can you show that part of the .config file..? – MethodMan Feb 06 '15 at 16:47
  • 1
    This should be closed against the canonical NRE question. You really need to learn how to debug simple exceptions, and a NRE is the simplest of the bunch. Something is null, make it not so, poof, done. –  Feb 06 '15 at 17:03
  • @Will: I agree. But, should you not go ahead and _vote_ accordingly? :) – Peter Duniho Feb 07 '15 at 04:58
  • @PeterDuniho It *was* closed as dupe. Others voted to reopen. I could close it again with a single vote, but after it was reopened by the votes of multiple people that's a bit too dickish even for me. OP needs to learn how to debug NREs, however. –  Feb 08 '15 at 17:28
  • @Will: ah, I see. Is there a way to see the close history of a question? I obviously had no idea the question had already been closed once (and it boggles the mind that anyone would have voted to reopen). I agree 100% with what you wrote. – Peter Duniho Feb 08 '15 at 18:04
  • @PeterDuniho for support questions, check out [meta]. Let's not bug OP anymore with our conversation, if you have more questions. You can see the close history by looking at the edit history. stackoverflow.com/posts/[post id]/revisions. The link is usually to the left of the OP's identity at the bottom of the question, and says "edited blah blah" –  Feb 08 '15 at 21:06

0 Answers0