Mokey Mokey - 1 year ago 105
SQL Question

C#: Hash Multiple Strings from a Form to Class, back to the form to SQL

I'm trying to figure out a way to do this.

I want to hash with a salt from my main form and post it to an SQL Server, My issue is currently I cannot seem to figure out how to call the hashed code for an SQL statement.

The issue is I can't seem to figure out how to call the hashed string into a variable or so. One of the errors I get is:


An Object of reference for the non-static field, method, or
property'Functions.UniqueID'


If I tried
Functions myFunction = new Functions();


I got:


There is no argument given that corresponds to the required formal
parameter


I tried looking it up, but really just came up blank since I'm new to coding and don't quite understand all the terminology everyone throws out.

The reason why I was wanting it in a class was that I will be using this hash/salt in multiple instances of the application.

Let me show the code and maybe someone can help:

Main Form:
Getters and Setters:


public string IdentifyOrder
{
get { return txtOrder.Text; }
set { txtOrder.Text = value; }
}
public string IdentifyStandard
{
get { return cmbStandard.Text; }
set { cmbStandard.Text = value; }
}
public string IdentifyNote
{
get { return cmbNote.Text; }
set { cmbNote.Text = value; }
}


SQL Statement:

private void btnSubmitInfo_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection(Connection.MTRDataBaseConn))
{

con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT INTO dbo.[myDatabase] ([Purchase Order], [Standard], [Notes], [Unique Identifier]) VALUES(@PurchaseOrder,@Standard,@Notes,@UniqueIdentifier)";

cmd.Connection = con;

SqlParameter pPurchaseOrder = new SqlParameter("@PurchaseOrder", SqlDbType.VarChar, 50);
SqlParameter pStandard = new SqlParameter("@Standard", SqlDbType.VarChar, 50);
SqlParameter pNotes = new SqlParameter("@Notes", SqlDbType.VarChar, 50);
SqlParameter pUID = new SqlParameter("@UniqueIdentifier", SqlDbType.VarChar, 50);


pPurchaseOrder.Value = txtPurchaseOrder.Text;
pStandard.Value = cmbStandard.Text;
pNotes.Value = txtNotes.Text;
pUID.Value = Functions.UniqueID;

cmd.Parameters.Add(pPurchaseOrder);
cmd.Parameters.Add(pStandard);
cmd.Parameters.Add(pNotes);
cmd.Parameters.Add(pUID);

//execute
cmd.ExecuteNonQuery();

}
}
catch (SqlException ex)
{
//catch error
MessageBox.Show(ex.Message);
}
}


My Class"Functions":

public class Functions
{
public readonly MainForm Identifiers;
public Functions(MainForm Identifiers)
{
this.Identifiers = Identifiers;
}

public void GenerateUniqueIdentifier()
{
string orderID = Identifiers.IdentifyOrder;
string standardID = Identifiers.IdentifyStandard;
string noteID = Identifiers.IdentiftNote;

string salt = "" + orderID + "" + standardID + "" + noteID + "";
}

public String GenHash(String input, String salt)
{
byte[] bytes = Encoding.UTF8.GetBytes(input + salt);
System.Security.Cryptography.SHA256Managed sha256hashstring =
new System.Security.Cryptography.SHA256Managed();
byte[] hash = sha256hashstring.ComputeHash(bytes);

return Convert.ToBase64String(hash);
}

public string UniqueID { get; set; }

}

Answer Source

You need to create an object of Functions class and then call methods on it or access properties of it.

Something like as following.

//Previous code...

var functionsObj = new Functions(mainForm); // You need to pass the
     // object of class MainForm as argument to Functions constructor 
     //If this code is running in code behind for "MainForm" then you can do as following.
var functionObj = new Functions(this);
pPurchaseOrder.Value = txtPurchaseOrder.Text;
pStandard.Value = cmbStandard.Text;
pNotes.Value = txtNotes.Text;
pUID.Value = functionsObj.UniqueID;

//Next Code....

This should resolve the issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download