how to add datas from a web page to a data base using C# in asp.net
(a symple mathod)

Dani AI

Generated

A short, practical workflow for taking values from an ASP.NET page and inserting them into a SQL database: collect control values on submit, validate and convert them server-side, keep the connection string in web.config, and execute a parameterized INSERT (or stored procedure) inside using-blocks so connections are always disposed. This expands on 's call for more details and ties together 's INSERT suggestion and 's binary-upload example.

Typical steps

  • Identify which controls supply data (TextBox, DropDownList, CheckBox, FileUpload, GridView edit, etc.).
  • Validate and parse server-side (use TryParse for numbers/dates; trim strings).
  • Store the connection string in web.config and retrieve it with ConfigurationManager.
  • Use parameterized SqlCommand (or stored procs) — never concatenate user input into SQL.
  • For files/images use FileUpload to read bytes and store as varbinary(max) with SqlDbType.VarBinary.
  • Wrap DB calls in using blocks and handle exceptions to surface meaningful errors (and log them).

Minimal example (web.config + button handler)

<connectionStrings>
  <add name="MyDb" connectionString="Data Source=SERVER;Initial Catalog=MyDatabase;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

protected void btnSave_Click(object sender, EventArgs e)
{
  string conn = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
  string sql = "INSERT INTO People (FirstName,LastName,Age) VALUES (@FirstName,@LastName,@Age)";

  using (var cn = new SqlConnection(conn))
  using (var cmd = new SqlCommand(sql, cn))
  {
    cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = txtFirstName.Text.Trim();
    cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = txtLastName.Text.Trim();

    int age;
    var pAge = cmd.Parameters.Add("@Age", SqlDbType.Int);
    pAge.Value = int.TryParse(txtAge.Text.Trim(), out age) ? (object)age : DBNull.Value;

    cn.Open();
    cmd.ExecuteNonQuery();
  }
}

Notes and troubleshooting

  • Parameterized commands prevent SQL injection.
  • Check connection string, DB permissions, and the SQL Server instance if connections fail.
  • Prefer stored procedures or an ORM for larger apps.
  • For binary data prefer varbinary(max) (modern SQL Server) instead of the older Image type; 's example is useful for file upload handling.
  • Validate inputs and handle DBNull for nullable columns to avoid type errors.

Recommended Answers

All 4 Replies

how to add datas from a web page to a data base using C# in asp.net
(a symple mathod)

Hi ChatuD,

Can you explain more? What are you using to get the data from the web page (DropDownList, CheckBox, TextBox, GridView, etc).? Give us more details about the context so we can give you more accurate suggestions. =)

Thanks,

Ana

you can open an sql connection and then use "Insert" commands to add it to database

If you want to be a good programmer one day, you need to read and practice and make research before you can ask someone. It seems that you did not even try , nor Google. the First thing you can do is to buy a book and read , this is a very basic question and it has been covered by almost all beginners book.

Here is an example of inserting an image from the hard drive. Obviously you want to change the input from a file to the controls you have on a page:

private static string BuildSqlNativeConnStr(string server, string database)
    {
      return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", server, database);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      byte[] bytes = File.ReadAllBytes(@"C:\picture.bmp");
      const string query = @"Insert Into Picture (Picture) Values (@Picture)";
      using (SqlConnection conn = new SqlConnection(BuildSqlNativeConnStr("apex2006sql", "Bugs")))
      {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
          SqlParameter parm = new SqlParameter("@Picture", SqlDbType.Image);
          parm.Value = bytes;
          cmd.Parameters.Add(parm);
          cmd.ExecuteNonQuery();
        }
      }
    }
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.