I'm creating a website using C#/ASP.NET and I keep getting the same error, time and time again and I do not know for the life of me how to fix it. I have been trying, but here's the code;
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- if (IsPostBack)
- {
- SqlConnection connec = new SqlConnection("Data Source=valve; Initial Catalog=m_soft20151_n0266710; Integrated Security=True");
- connec.Open();
- string cmdStr = "SELECT COUNT(*) FROM Users WHERE userName='" + TextBox1.Text + "'";
- SqlCommand findUser = new SqlCommand(cmdStr, connec);
- int temp = Convert.ToInt32(findUser.ExecuteScalar().ToString());
- connec.Close();
- if (temp == 1)
- {
- Response.Write("This username already exists.");
- }
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection connec = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
- connec.Open();
- string insCmd = "INSERT INTO Users ([userName], [Password], [E-mail]) VALUES (@userName, @Password, @E-mail)";
- SqlCommand insertUser = new SqlCommand(insCmd, connec);
- insertUser.Parameters.AddWithValue("@userName", TextBox1.Text);
- insertUser.Parameters.AddWithValue("@Password", TextBox2.Text);
- insertUser.Parameters.AddWithValue("@E-mail", TextBox4.Text);
- try
- {
- insertUser.ExecuteNonQuery();
- connec.Close();
- Response.Redirect("Login.aspx");
- }
- catch (Exception er)
- {
- Response.Write("Please ensure that all fields are completed.");
- }
Thanks in advance!