i have a table with certain column names for example book table with name,author and price now i have a web form with three text box i need to add the text box data to database from ASP.Net form pls tell the coding urgent!!!!

Dani AI

Generated

For : a minimal, safe Web Forms pattern to take three textboxes (name, author, price) and insert them into a Book table. As pointed toward parameters, this uses a parameterized INSERT, validates the price, and keeps the connection string in web.config. Replace control IDs and the connection name to match your page.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

protected void btnSave_Click(object sender, EventArgs e)
{
    string name = txtName.Text.Trim();
    string author = txtAuthor.Text.Trim();
    decimal price;
    if (!decimal.TryParse(txtPrice.Text.Trim(), out price))
    {
        lblMessage.Text = "Enter a valid price.";
        return;
    }

    string connStr = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
    string sql = "INSERT INTO Book ([Name], [Author], [Price]) VALUES (@name, @author, @price)";

    using (var conn = new SqlConnection(connStr))
    using (var cmd = new SqlCommand(sql, conn))
    {
        cmd.Parameters.Add("@name", SqlDbType.NVarChar, 200).Value = name;
        cmd.Parameters.Add("@author", SqlDbType.NVarChar, 200).Value = author;
        var p = cmd.Parameters.Add("@price", SqlDbType.Decimal);
        p.Value = price;
        p.Precision = 9;
        p.Scale = 2;

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

    lblMessage.Text = "Saved.";
}

Put the connection string in web.config:

<connectionStrings>
  <add name="MyDb" connectionString="Data Source=SERVER;Initial Catalog=YourDatabase;Integrated Security=True;" />
</connectionStrings>

Notes and common pitfalls: always validate user input (server-side), avoid concatenating SQL (prevents SQL injection), prefer explicit parameter types and sizes rather than AddWithValue, set precision/scale for decimals if your column requires it, and wrap DB calls in try/catch in real apps to log exceptions. For larger projects consider stored procedures or an ORM (Entity Framework) for cleaner data access.

Recommended Answers

All 2 Replies

The simplest solution would to do a google search for "submitting forms in ASP.NET"

i have a table with certain column names for example book table with name,author and price now i have a web form with three text box i need to add the text box data to database from ASP.Net form pls tell the coding urgent!!!!

on button1_click......
u should try parameters with string s = ......

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.