I got this code off a site, with a really nice layout and style, I've never worked with input field, so I'm stuck how can I save this to a database.
I tried changing everything to a <asp:Label> and <asp:TextBox> but then I do not get the same layout.

<body>
  <div id="text">
    <div class="body_resize">
      <div class="about">
        <h2>Registration</h2>
        <form  method="post" id="contactform">
          <ol>
            <li>
              <label for="firstName">Name</label>
              <input id="firstName" name="nameTextBox" class="text" />
            </li>
            <li>
              <label for="country">Country</label>
              <input id="country" name="countryTextBox" class="text" />
            </li>
            <li>
              <label for="email">E-Mail Address</label>
              <input id="email" name="emailTextBox" class="text" />
            </li>
            <li>
              <label for="email">Username</label>
              <input id="user" name="usernameTextBox" class="text" />
            </li>
            <li>
              <label for="pass1">Password</label>
              <input id="pass1" name="pass1TextBox" class="text" />
            </li>
            <li>
              <label for="pass2">Confirm Password</label>
              <input id="pass2" name="pass2TextBox" class="text" />
            </li>
            <li class="buttons">
            <input type="image" name="imageField" id="imageField" src="images/send.gif" onclick="Submit" />
          </li>
          </ol>
        </form>
      </div>
    </div>
  </div>  
  <label for="test"></label>
</body>

I tried this to test:

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

    protected void Submit()
    {
        test.Text = "Working";
    }
}

but I receive an error: The name 'test' does not exist in the current context

kvprajapati commented: Read forum rules. Thread belongs to ASP.NET -2

Where did you define test?

is test a label or textbox?

Ok I just reread what you said.

You want to insert the records from the first code.

Where is you connection to the database?

What database do you have?

If you think I didn't understand what you mean please clarify.

Hey, this is an update of my code:

<body>
  <div id="text">
    <div class="body_resize">
      <div class="about" >
        <h2>Registration</h2>
        <form  method="post" id="contactform">
          <ol>
            <li>
              <label for="firstName" >Name</label>
              <input id="firstName" runat="server" name="nameTextBox" class="text" />
            </li>
            <li>
              <label for="country">Country</label>
              <input id="country" runat="server" name="countryTextBox" class="text" />
            </li>
            <li>
              <label for="email">E-Mail Address</label>
              <input id="email" runat="server" name="emailTextBox" class="text" />
            </li>
            <li>
              <label for="email">Username</label>
              <input id="user" runat="server" name="usernameTextBox" class="text" />
            </li>
            <li>
              <label for="pass1">Password</label>
              <input id="pass1" runat="server" name="pass1TextBox" class="text" />
            </li>
            <li>
              <label for="pass2">Confirm Password</label>
              <input id="pass2" runat="server" name="pass2TextBox" class="text" />
            </li>
          </ol>
        </form>
      </div>
    </div>
    <form id="form1" runat="server">
            <asp:ImageButton ID="submitButton"
                runat="server" src="images/send.gif" OnClick="submitButton_Click" /><br />
            <asp:Label ID="message" runat="server" />
   </form>
public partial class Register : System.Web.UI.Page
{
    string scon =
        "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|db_TrainStation.mdb";

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void submitButton_Click(object sender, ImageClickEventArgs e)
    {
        string str =
           "INSERT INTO ts_registration(r_name, r_country, r_email, r_username, r_pass1, r_pass2)values(r_name, r_country, r_email, r_username, r_pass1, r_pass2)";
        string id = "Select @@Identity";
        int r_id;
        using (OleDbConnection con = new OleDbConnection(scon))
        {
            using (OleDbCommand cmd = new OleDbCommand(str, con))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("r_name", firstName.Value);
                cmd.Parameters.AddWithValue("r_country", country.Value);
                cmd.Parameters.AddWithValue("r_email", email.Value);
                cmd.Parameters.AddWithValue("r_username", user.Value);
                cmd.Parameters.AddWithValue("r_pass1", pass1.Value);
                cmd.Parameters.AddWithValue("r_pass2", pass2.Value);
                con.Open();
                cmd.ExecuteNonQuery();
                cmd.CommandText = id;
                r_id = (int)cmd.ExecuteScalar();
            }
        } 
    }
}

When the submit button is pressed, a new record in the database is created but there are no values from the input fields, they are all empty.

A couple of things are bothering me about the code above from what I'm reading.

  1. the form tag only encompasses the submit button
  2. the only asp compatible element in the page is the submit button

While you have set the cmd.Parameters to pull values from the various inputs on the page, I don't believe elements that are outside of the <form></form> constructs will be actively read by the C# code-behind.

I believe that, as much as you 'like' the way the page looks with the current layout, you may need to convert the inputs into proper asp inputs and enclose them within the form for this to work properly.

May be the issue is in form tag. The first thing I suggest you is try using simpler approach, i.e. using single form control and placing other server control inside it. I guess it is only the design issue that bothers you.

But if you really need to does this in your way then try using this approach.

1) Remove runat=server tag from your input control. Add a hidden field and then insert some JS in following way.

<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
    	function ExtractData() {
    		var strValue = GetString();
    		alert(strValue);
    		if (strValue != "ERROR") {
    			E("<%=hdnData.ClientID %>").value = strValue;
    			return true;
    		} else {
    			return false;
    		}
    	}
    	function GetString() {
    		if ((E("pass2").value != E("pass1").value) || (E("pass2").value == "")) {
    			return "ERROR";
    		} else {
    			return "Name:" + E("firstName").value + ";Country:" + E("country").value + ";Email:" + E("email").value + ";UserName:" + E("user").value + ";PassWord:" + E("pass1").value + ";Confirm:" + E("pass2").value;
    		}
    	}
    	
    	function E(id) {
    		return document.getElementById(id);
    	}
    	
    </script>
</head>
<body>
  <div id="text">
    <div class="body_resize">
      <div class="about" >
        <h2>Registration</h2>
        <form  method="post" id="contactform">
          <ol>
            <li>
              <label for="firstName" >Name</label>
              <input id="firstName" name="nameTextBox" class="text" />
            </li>
            <li>
              <label for="country">Country</label>
              <input id="country" name="countryTextBox" class="text" />
            </li>
            <li>
              <label for="email">E-Mail Address</label>
              <input id="email" name="emailTextBox" class="text" />
            </li>
            <li>
              <label for="email">Username</label>
              <input id="user" name="usernameTextBox" class="text" />
            </li>
            <li>
              <label for="pass1">Password</label>
              <input id="pass1" name="pass1TextBox" class="text" />
            </li>
            <li>
              <label for="pass2">Confirm Password</label>
              <input id="pass2" name="pass2TextBox" class="text" />
            </li>
          </ol>
        </form>
      </div>
    </div>
    <form id="form1" runat="server">
            <asp:ImageButton ID="submitButton"  runat="server" src="images/send.gif" 
				OnClientClick="return ExtractData();" onclick="submitButton_Click" /><br />
                <asp:HiddenField ID="hdnData" Value="" runat="server" />
            <asp:Label ID="message" runat="server" />
   </form>
   </body>

2) Add a function to extract the value and use it in the way shown below.

protected void Page_Load(object sender, EventArgs e)
    {

    }
	private string GetStr(string title)
	{
		string[] mystr = hdnData.Value.Split(";".ToCharArray());
		string value = string.Empty;
		foreach (string Str in mystr)
		{
			string[] namePair = Str.Split(":".ToCharArray());
			if (namePair[0] == title)
			{
				value = namePair[1];
				break;
			}
		}
		return value;
	}

	protected void submitButton_Click(object sender, ImageClickEventArgs e)
	{
		Response.Write("Name is :" + GetStr("Name"));
		Response.Write("Country is :" + GetStr("Country"));
		Response.Write("Email is :" + GetStr("Email"));
		Response.Write("UserName is :" + GetStr("UserName"));
		Response.Write("Password is :" + GetStr("PassWord"));
		Response.Write("Confirm is :" + GetStr("Confirm"));

	}
commented: Great post! +1

Thanks prashantchalise,
I will try this.

Thanks it works!

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.