Creating a Simple Invoice in ASP.NET C# with Database
Here is an example of how you can create a simple invoice in ASP.NET C# with a database:
// First, let's assume you have a database table called Invoices with the following fields:
// - InvoiceId (primary key)
// - CustomerName
// - InvoiceDate
// - TotalAmount
// 1. Create an ASP.NET web form (e.g., InvoiceForm.aspx) with the necessary input fields and a button to create the invoice.
// 2. Retrieve the form data and save it to the database when the button is clicked:
protected void btnCreateInvoice_Click(object sender, EventArgs e)
{
// Retrieve input values from the form
string customerName = txtCustomerName.Text;
DateTime invoiceDate = DateTime.Now;
decimal totalAmount = Convert.ToDecimal(txtTotalAmount.Text);
// Save the data to the database using ADO.NET or an ORM like Entity Framework
using (SqlConnection connection = new SqlConnection("YourConnectionString"))
{
string query = "INSERT INTO Invoices (CustomerName, InvoiceDate, TotalAmount) VALUES (@CustomerName, @InvoiceDate, @TotalAmount)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@CustomerName", customerName);
command.Parameters.AddWithValue("@InvoiceDate", invoiceDate);
command.Parameters.AddWithValue("@TotalAmount", totalAmount);
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
}
3. Once the invoice is created and saved in the database, you can retrieve it and display it on another page or in a printable format:
// Create a new web form (e.g., InvoiceDetails.aspx) to display the invoice details.
// In the Page_Load event of InvoiceDetails.aspx, retrieve the invoice details from the database based on the requested InvoiceId:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Retrieve the requested InvoiceId from the query string or any other parameter
int invoiceId = Convert.ToInt32(Request.QueryString["invoiceId"]);
// Retrieve the invoice details from the database
Invoice invoice = GetInvoiceById(invoiceId); // This method fetches the invoice details from the database based on the InvoiceId
// Display the invoice details on the page
lblCustomerName.Text = invoice.CustomerName;
lblInvoiceDate.Text = invoice.InvoiceDate.ToShortDateString();
lblTotalAmount.Text = invoice.TotalAmount.ToString();
}
}
4. Finally, you can style the InvoiceDetails.aspx as needed using CSS to make it look like a printable invoice.