I’ve created 2 tables in a database, each table has a CompanyID field that’s in relationship with eachother
In my registration page the company has to register the following info:
- CompanyID
- Company Name
- Business Type
- Contact Person
- Telephone
- Address
- Password
The company then logs in using their Company Name and Password, once logged in a company can post a job and the problem I have is in my jobtable there is also a field called CompanyID
How can I retrieve the CompanyID from the already registered companys stored in my Company table and use it
SqlParameter p6 = new SqlParameter("CompanyID", 'here' );
my full code:
protected void Button1_Click(object sender, EventArgs e)
{
string strcon = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\VC_temps.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(strcon);
SqlCommand com = new SqlCommand("Store-Jobs", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("Job", TextBox1.Text);
SqlParameter p2 = new SqlParameter("JobType", DropDownList1.Text);
SqlParameter p3 = new SqlParameter("StartDate", TextBox3.Text);
SqlParameter p4 = new SqlParameter("Time", TextBox2.Text);
SqlParameter p5 = new SqlParameter("JobID", TextBox1.Text.Substring(3).ToUpper());
SqlParameter p6 = new SqlParameter("CompanyID", );
SqlParameter p7 = new SqlParameter("PositionFilled", "NO");
SqlParameter p8 = new SqlParameter("Description", TextBox4.Text);
com.Parameters.Add(p1);
com.Parameters.Add(p2);
com.Parameters.Add(p3);
com.Parameters.Add(p4);
com.Parameters.Add(p5);
com.Parameters.Add(p6);
com.Parameters.Add(p7);
com.Parameters.Add(p8);
con.Open();
com.ExecuteNonQuery();
Labelinfo.Text = "Post successful.";
}
There are several ways to do this. Since the user is logging in to a specific company then I am assuming the CompanyId would be the same during the entire session. Therefore I would recommend retrieving the CompanyId during the login process and storing it into the Session variable. Then you get the CompanyId from this session variable anytime you wish.
An alternative would be to pass the company name into the stored procedure and let the stored procedure get the CompanyId using the Company Name value.
On a secondary note….I like to show best practices when I see code that could have problems. Your code will work fine if everything succeeds but if exceptions occur then you will leave SQL connections open. I have taken the liberty to refactor your code to show an alternative…