Hi guys,

I coding a page to save the first name in the database, iam having a problem and I want you to help me please..

aspx page code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CompanyInfo.aspx.cs" Inherits="CompanyInfo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    
<html  xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <span style="font-size: 36pt"><strong><span style="text-decoration: underline">Comany
            registration</span></strong><br />
        </span>
        <br />
       First Name:
        <asp:TextBox ID="NameTextBox" runat="server"></asp:TextBox>
        
        <asp:Button ID="SubmitButton" runat="server" Text="Submit" />

     </div>  
    </form>
</body>
</html>

and this is the code behind it

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

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

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

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

        SqlConnection conn = new SqlConnection();
        DateTime laiks = new DateTime();
        conn.ConnectionString = "data source=.\\SQLEXPRESS;initial catalog=CV;integrated security=true;";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Test(Firstname) values ('" & NameTextBox.Value & "')";
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
  }

I have the error in "Value" which is attached with "NameTextBox".
I have a database which is called "Test" and table with "Test" naming.
The colume name is "Firstname" with varchar(50) type
thanks in advance

Try replacing NameTextBox.Value with NameTextBox.Text. ASP server control textboxes use the .Text property. If the control was an html server control (input tag with a runat="server" attribute), then you would use the .Value property.

also, just as a formatting rule, try to keep your control names consistent

i.e. tbFirstName

otherwise, it could get a little messy trying to find all your textboxes on a page

Hi guys,

I coding a page to save the first name in the database, iam having a problem and I want you to help me please..

aspx page code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CompanyInfo.aspx.cs" Inherits="CompanyInfo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html  xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>

    <form id="form1" runat="server">
    <div>
        <span style="font-size: 36pt"><strong><span style="text-decoration: underline">Comany
            registration</span></strong><br />
        </span>
        <br />
       First Name:
        <asp:TextBox ID="NameTextBox" runat="server"></asp:TextBox>

        <asp:Button ID="SubmitButton" runat="server" Text="Submit" />

     </div>  
    </form>
</body>
</html>

and this is the code behind it

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

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

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

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

        SqlConnection conn = new SqlConnection();
        DateTime laiks = new DateTime();
        conn.ConnectionString = "data source=.\\SQLEXPRESS;initial catalog=CV;integrated security=true;";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Test(Firstname) values ('" & NameTextBox.Value & "')";
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
  }

I have the error in "Value" which is attached with "NameTextBox".
I have a database which is called "Test" and table with "Test" naming.
The colume name is "Firstname" with varchar(50) type
thanks in advance

The button is missing the OnClick Event handler. To add an event handler to to a button just double click on the button from the design view and the VisualStudio IDE should automatically take you to the CS/VB part of the page with the relevant functions generated.

In this case you could manually add OnClick event like this

      <asp:Button ID="SubmitButton" runat="server" OnClick="submitButton_Click" Text="Button" />

now the button click event will invoke the function submitButton_Click.

And of course

cmd.CommandText = "INSERT INTO Test(Firstname) values ('" & NameTextBox.Value & "')";

should be

cmd.CommandText = "INSERT INTO Test(Firstname) values ('" & NameTextBox.Text & "')";

thanks the problem has been solved

thanks very much for your explanation

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.