Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need to program a button to save what write in textbox in database

What I have tried:

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

namespace T4
{
    public partial class Person_details : System.Web.UI.Page
    {



        public static string con = ConfigurationManager.ConnectionStrings["Dsn=SQL32;uid=sa;pwd=Abc@123123"].ConnectionString;



        protected void Page_Load(object sender, EventArgs e)

        {



        }



        protected void addBtn_click(object sender, EventArgs e)

        {

            OdbcConnection con1 = new OdbcConnection(con);

            try

            {

                con1.Open();



                OdbcCommand insertCmd = new OdbcCommand("insert into person1 values(?,?,?)", con1);

                insertCmd.Parameters.Add(new OdbcParameter("@2", Name.Text));

                insertCmd.Parameters.Add(new OdbcParameter("@3", Mobile.Text));

                insertCmd.Parameters.Add(new OdbcParameter("@4", Email.Text));

                int rowseeffected = insertCmd.ExecuteNonQuery();



                insertCmd = new OdbcCommand("insert into Orders values(?,?,?)", con1);

                insertCmd.Parameters.Add(new OdbcParameter("@5", int.Parse(TextBox4.Text)));

                insertCmd.Parameters.Add(new OdbcParameter("@6" , CheckBoxList1.SelectedItem.Text ));

                insertCmd.Parameters.Add(new OdbcParameter("@7", FileUpload1.FileName));

                rowseeffected = insertCmd.ExecuteNonQuery();

            }

            catch (OdbcException ex)

            {

                throw new FaultException("Databace Connection Error Contact With System Administrator", new FaultCode("0"));

            }

            catch (Exception ex)

            {

                throw new FaultException(ex.Message, new FaultCode("1"));

            }

            finally

            {

                con1.Close();



            }

        }


    }
}
Posted
Updated 3-Jul-21 23:22pm
v2
Comments
Richard MacCutchan 22-Jun-21 6:01am    
"I tried writing more than one code but it didn't work for me"
And you think maybe we can guess what you did and provide a solution to code that we cannot see?

Please think before you post a question:
What actual code are you using - we need to see it, we have no way of guessing.
What happened when you compiled it - did you get any error messages - we need to see them.
What happened when you ran it - give full details, do not say "it didn't work".

1 solution

Add your button.
Add a Click handler to your button.
Then in the click handler, read the text from the TextBox.
Then all you have to do is an SQL INSERT to add it to your DB.

Exactly how you INSERT will depend on what DB system you are using, but this will save a string and a DateTime value to SQL Server:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1Name, myColumn2Name) VALUES (@STR, @DT)", con))
        {
        cmd.Parameters.AddWithValue("@STR", strReadFromTextBox);
        cmd.Parameters.AddWithValue("@DT", DateTime.Now);
        cmd.ExecuteNonQuery();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900