saving data into sql database from textboxes asp.net C#

how to insert data from textbox to database in c#
how to save data in database using c#
c# code for inserting data into sql database
how to insert data into database using c#
how to insert data in database in asp.net using c#
how to save data from html form to a database using c#
how to create database in asp net
how to update data in asp net using c#

I have tried so many ways to save textbox text into an SQL database. I just want to save some text from textboxes into an SQL database. I am using Microsoft Visual Studio 2010 and C# programming language. Here is my current code:

protected void BtnSave_Click(object sender, EventArgs e)
    {
        string Name = TxtFirstName.Text;
        string Address = TxtDOB.Text;
        string DOB = TxtDOB.Text;
        string Occupation = TxtOccupation.Text;
        string Home = TxtHome.Text;
        string Work = TxtWork.Text;
        string Mobile = TxtMobile.Text;
        string Email = TxtEmail.Text;
        string SportActivities = TxtSportActivities.Text;

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString)) ;
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES ( @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)")) ;
            {
                cmd.connection = con;
                con.Open();
                cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
                cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
                cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
                cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
                cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
                cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
                cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
                cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
                cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
                cmd.ExecuteNonQuery();
                con.close();
            }
        }
    }

protected void Button1_Click(object sender, EventArgs e)
        {
            string Name = TxtFirstName.Text;
            string Address = TxtDOB.Text;
            string DOB = TxtDOB.Text;
            string Occupation = TxtOccupation.Text;
            string Home = TxtHome.Text;
            string Work = TxtWork.Text;
            string Mobile = TxtMobile.Text;
            string Email = TxtEmail.Text;
            string SportActivities = TxtSportActivities.Text;

            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientDetailsConnectionString"].ConnectionString);

            SqlCommand cmd = new SqlCommand("INSERT INTO [ClientDetails] ([ID], [Name], [Address], [DOB], [Occupation], [Home], [Work], [Mobile], [Email], [SportActivities]) VALUES (@ID, @Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities)");

            cmd.Connection = con;
            con.Open();
            cmd.Parameters.AddWithValue("@ID", TxtID.Text);
            cmd.Parameters.AddWithValue("@Name", TxtFirstName.Text);
            cmd.Parameters.AddWithValue("@Address", TxtAddress.Text);
            cmd.Parameters.AddWithValue("@DOB", TxtDOB.Text);
            cmd.Parameters.AddWithValue("@Occupation", TxtOccupation.Text);
            cmd.Parameters.AddWithValue("@Home", TxtHome.Text);
            cmd.Parameters.AddWithValue("@Work", TxtWork.Text);
            cmd.Parameters.AddWithValue("@Mobile", TxtMobile.Text);
            cmd.Parameters.AddWithValue("@Email", TxtEmail.Text);
            cmd.Parameters.AddWithValue("@SportActivities", TxtSportActivities.Text);
            cmd.ExecuteNonQuery();
            con.Close();
        }

You should add parameter for each of these values:

@Name, @Address, @DOB, @Occupation, @Home, @Work, @Mobile, @Email, @SportActivities

You used cmd.Parameters.AddWithValue("@Name", SqlDbType.Text); only for name.

EDIT:

There are 2 ways to provide data to sql: 1) Adding parameter by name and later it's value:

 command.Parameters.Add("@name", SqlDbType.Int);
        command.Parameters["@name"].Value = nametextbox.text;

2) Adding parameter with value

command.Parameters.AddWithValue("@name", nametextbox.text);

Please Use one of them. Not the mix.

EDIT REGARDING ERROR:

If you use OnClick attribute in the page's HTML view, the name is expected to be on the page script and not in the code-behind. If you want to use a handler in the code-behind, remove OnClick attribute, go to design view and double-click the button. The code-behind will open on the right function. Or you can select event handler in the button properties panel on Events tab. Or you can do it without design view whatsoever. Just go to the code-behind and add a line for registering the event-handling method for the Click event of the button: btnSignIn.Click += new EventHandler(this.btnSignIn_Click);

When you do it in the design view, this line is added to the automatically generated InitializeComponent() method.

Save data from textbox to database, As you are saying you have successfully shown the data on textboxes and now you are trying to update same data, ok for that you have need to  Search for Free Sql Databases on the New Internetcorkboard.com

Click in front of brackets, a red point will come there and again start your process

C# windows form tutorial insert/save text-box data to MS SQL , aspx page.codes can be vb or c#.it is no important.I just want sending textboxs datas to database with a way.Please help me. eTour.com is the newest place to search, delivering top results from across the web. Content updated daily for web sql database.

I think you have lost the link between the button press and the event. You can check this using the Properties Window.

  1. display the page containing the asp:Button
  2. select the asp:Button
  3. Look at the event properties for the asp:button

My guess is that you have lost the link. So, comment out the event response code and recreate the event. Use the same process you used to create the event originally. Then, cut and paste the commented out code you need from the commented out event method. Remove the comment indicators from the pasted code. Your event should now work.

submit data from textbox to sql database, Here Mudassar Ahmed Khan has explained how to save (insert) dynamic TextBox Value (Text) to SQL Server database table in ASP.Net using C# and VB.​Net. Search for Free Sql Databases on the New KensaQ.com

Save (Insert) dynamic TextBox Value (Text) to database in ASP.Net , This article shows how to insert data into a database using ASP. I used several textboxes and a button. When the user clicks on the “Insert” button the data is saved into the database. In Solution Explorer you get your empty website, then add a Web Form and SQL Server Database as in the following. Fast answers on Teoma.us! Find web sql database. Content updated daily for web sql database.

Insert Data Into Database by Stored Procedure in ASP.Net C#, In this I have used SqlDataAdapter, SqlConnection to save the data. insert the textbox value into sql server database in asp.net using c#.net. Saving values of dynamically created TextBoxes to database in ASP.Net When the Save Button is clicked, a loop is executed over all the TextBoxes present inside the Panel control. Inside the loop, the value of each TextBox is fetched and inserted into SQL Server database table.

Save Or Insert TextBox Value into Sql DataBase Table In Asp.Net , The SQL Insert Into statement; The Validation helper. In most apps, that's not a practical way to put data into the database, though. you'll create a web-based interface that lets you or anyone enter data and save it to the database. It uses <input> elements for the text boxes and for the submit button. Here Mudassar Ahmed Khan has explained with an example, how to retrieve data from Database and set the value in TextBox in ASP.Net using C# and VB.Net. This article will illustrate how to fetch records (data) from SQL Server Database using SqlDataReader and then set the retrieved values in TextBoxes in ASP.Net using C# and VB.Net.

Comments
  • all parameters required. post your error message.
  • there is no error message.
  • add a breakpoint to check weather click event is being called or not ?
  • use try..catch to catch exception, for cmd.Parameters.AddWithValue("@Name", SqlDbType.Text) pass the value
  • @HenryWater check answer for breakpoint
  • How can we know. It looks ok. Add all others and check. = )
  • changed my code and added the other parameters. still not working. There is no error message
  • Please edit Question and Add new code there so we can validate.
  • i eddited my answer, check it and correct mistakes.
  • @HenryWater : regarding error in Question: have you cheked that in the aspx page the button oncClick event is the same name as in the code behind? It sounds to me like they are different, and that is why you are getting the "cannot find" error.
  • your event is not being called. double click on button in design view and see where it is going
  • you were right. it was going somewhere else. Now i have moved the code and i have a error showing.