I have been working on a sign Up page for my web app, however I am facing issues with connecting my database to my web app. The frontend coding is proper and has no errors
This is the backend code inside the Onclick method:
SqlConnection conn = new SqlConnection(@"Data Source=TARISAI;"
"Initial Catalog=FarmCentral;"
"Integrated Security=SSPI;");
try
{
//SqlConnection conn = new SqlConnection(strcon);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
//insert values from web application to SQL database
SqlCommand cmd = new SqlCommand("INSERT INTO employee_tbl (full_name, date_of_birth, contact, email, province, city, postcode, address, employeeID, Password) "
"values(@full_name, @date_of_birth, @contact, @email,@province, @city, @postcode, @address, @employeeID, @Password)", conn);
// link data to textboxes
cmd.Parameters.AddWithValue("@full_name", namebox.Text.Trim());
cmd.Parameters.AddWithValue("@date_of_birth", dobbox.Text.Trim());
cmd.Parameters.AddWithValue("@contact", contactbox.Text.Trim());
cmd.Parameters.AddWithValue("@email", emailbox.Text.Trim());
cmd.Parameters.AddWithValue("@province", provincedrop.SelectedItem.Value);
cmd.Parameters.AddWithValue("@city", citybox.Text.Trim());
cmd.Parameters.AddWithValue("@post code", postcodebox.Text.Trim());
cmd.Parameters.AddWithValue("@employeeID", employeeIDbox.Text.Trim());
cmd.Parameters.AddWithValue("@Password", passbox.Text.Trim());
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert('Sign Up Successful. Go to Employee Login to Login');</script>");
}
catch(Exception exx)
{
Response.Write("<script>alert('" exx.Message "');</script>");
}
When I click sign up it refreshes the page but does not do the action. There is no prompt and values are not added to database. Am I missing something or have I done it wrong
CodePudding user response:
Ok, do you have sql server express running?
Do you have sql studio installed?
Really, if not, then get the above working. it will help you oh so much.
next up:
Building the connecting string?
Let the system do that for you.
thus, you don't type in that connection, but BETTER is the Visual Studio builders can "make" and "create" the connection for you. And a nice touch is it ALSO lets you click on test connection.
Even better?
Your connection string now does NOT have to be in code. Again saving the world and your keyboard here.
So, in your web project, go here:
With above, it REALLY easy, since you just follow the prompts. So, click on the [...], and then you can connect to the sql server.
So, we click on above, and get this:
So, now it all drop downs - lots of hand holding here.
I get to click on servers - a drop down selection appears.
Same goes for databases:
eg this:
And after I select the database, I can click on test conneciton.
eg this:
So, now when done, i have this:
Ok, so now in code?
Say this:
//insert values from web application to SQL database
string strSQL
= "INSERT INTO employee_tbl (full_name, date_of_birth, contact, email, province, city, postcode, "
"address, employeeID, Password) "
"values (@full_name, @date_of_birth, @contact, @email,@province, @city, @postcode, @address, @employeeID, @Password)";
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmd = new SqlCommand(strSQL, conn))
{
cmd.Parameters.Add("@full_name", SqlDbType.NVarChar).Value = namebox.Text;
cmd.Parameters.Add("@date_of_birth", SqlDbType.Date).Value dobbox.Text;
cmd.Parameters.Add("@contact", SqlDbType.NVarChar).Value = contactbox.Text;
cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = emailbox.Text;
cmd.Parameters.Add("@province", SqlDbType.NVarChar).Value = provincedrop.SelectedItem.Value;
cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = citybox.Text;
cmd.Parameters.Add("@post code", SqlDbType.NVarChar).Value = postcodebox.Text;
cmd.Parameters.Add("@employeeID", SqlDbType.Int).Value = employeeIDbox.Text;
cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = passbox.Text;
conn.Open();
cmd.ExecuteNonQuery();
}
}
so, note how I used that connection string we setup (thus only ONE place in the applcation is required - allows you to change it with ease).
And you NEVER have to test if the connection is open - if you do, then your code is messed up, and did not follow the above pattern.