Hi all,
Im working on a page that sends information to a mssql database. Its a basic registration page with a UserID, Password, First Name, Middle Name, Last Name, and Email box. When Sign up is clicked, all the information in the textboxes should be sent to the Users table in the MSSQL Database. The Problem I am facing is that the data is not appearing in the tables after the sign up button is clicked. Why is it not working? Thanks in Advance!

Sign Up Page Code:

using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data.SqlClient;
using System.Drawing;
using System.Configuration;

namespace WebApplication3
{
    public partial class Signup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString);
        SqlCommand cmd = new SqlCommand("INSERT INTO Users(UserID, Password, FirstName, MiddleName, LastName, Email) VALUES (@UserID, @Password, @FirstName, @MiddleName, @LastName, @Email)", conn);

        cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.AddWithValue("@UserID", TextBox7.Text);
            cmd.Parameters.AddWithValue("@Password", Textbox2.Text);
            cmd.Parameters.AddWithValue("@FirstName", TextBox3.Text);
            cmd.Parameters.AddWithValue("@MiddleName", TextBox4.Text);
            cmd.Parameters.AddWithValue("@LastName", TextBox5.Text);
            cmd.Parameters.AddWithValue("@Email", TextBox6.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            Response.Redirect("login.aspx");
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString);
        conn.Open();
        SqlCommand cmd = new SqlCommand("select * from Users where UserID= '" + TextBox7.Text + "'", conn);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Label1.Text="UserName is Taken";
            this.Label1.ForeColor=Color.Red;
        }
        else
        {
            Label1.Text="UserName is not Taken";
            this.Label1.ForeColor = Color.Black;
        }


        }

Connection String:

<connectionStrings>
    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
      providerName="System.Data.SqlClient" />
    <add name="testConnectionString1" connectionString="Data Source=MALAYSIA\SQLEXPRESS;Initial Catalog=test;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

What the Page Looks Like:

73accb1346af3b00aadcce0ea2b51934

EDIT:
I forgot to mention that I have another page that uses the same connection string and similar code. Here is the code for that page(Incase it helps)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString1"].ConnectionString);
           SqlCommand cmd = new SqlCommand("INSERT INTO Observations(UserID, DateTime, Loc_Lat, Loc_Lon, Observation, Comments) VALUES (@UserID, @DateTime, @Loc_Lat, @Loc_Lon, @Observation, @Comments)", conn);
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Parameters.AddWithValue("@UserID", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Observation", TextBox2.Text);
            cmd.Parameters.AddWithValue("@DateTime", DateTime.Now);
            cmd.Parameters.AddWithValue("@Comments", TextBox3.Text);
            cmd.Parameters.AddWithValue("@Loc_Lat", TextBox4.Text);
            cmd.Parameters.AddWithValue("@Loc_Lon", TextBox5.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            Response.Redirect("Webform1.aspx");
        }
    }
}

What happens, aside from data not being written to the table? Any errors on the screen when you click on the submit button?

No there are no errors. It takes me to the login page when the button is clicked.

Nothing jumps out at me just from looking at your sample code... Are you sure you are looking at the correct data source to validate that nothing was written? I only ask because you have two data sources listed in your web.config so just want to make sure that you validated this. The two data sources point are not the same.

Yes im sure that the data source is correct. The Malaysia server has 2 tables, Observations and Users.

It is worth noting you never shut connections to the database, close the connection after each call, it could be causing a hidden error (though unlikely as it would be an exception)

Ok so I fixed it. Turns out that on the sign up button properties I had postbackurl set to the login.aspx page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.