File manager - Edit - G:/PleskVhosts/indiaminerals.in/vgm.INFOFIXDEVELOPERS.COM/admin/medical_master.aspx.cs
Back
using System; using System.Data; using System.Data.SqlClient; using System.IO; using System.Web.Configuration; using System.Web.UI; using System.Web.UI.WebControls; using WebApp.LIBS; namespace VGM.admin { public partial class medical_master : BasePageClass { string cs = WebConfigurationManager.ConnectionStrings["gav"].ConnectionString; SqlConnection con; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } private void BindGrid() { using (con = new SqlConnection(cs)) { con.Open(); using (SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM medicine order by Id", con)) { DataTable dt = new DataTable(); da.Fill(dt); Gridview1.DataSource = dt; Gridview1.DataBind(); } } } protected void submitButton_Click(object sender, EventArgs e) { string medicineName = medicine.Text; // Perform the database insertion. Replace this with your actual database insert logic. try { // Replace 'YourConnectionString' with your actual database connection string. using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["gav"].ConnectionString)) { connection.Open(); // Define an SQL query to insert the new medicine. string insertQuery = "INSERT INTO medicine (Medicine_name) VALUES (@MedicineName)"; using (SqlCommand command = new SqlCommand(insertQuery, connection)) { // Define the parameter for the MedicineName value. command.Parameters.AddWithValue("@MedicineName", medicineName); // Execute the insert command. command.ExecuteNonQuery(); Response.Write("<script>alert('Record Insert Successfully')</script>"); medicine.Text = ""; } } // After inserting, rebind the GridView to show the updated data. BindGrid(); SaveToCSV(); } catch (Exception ex) { Response.Write("<script>alert('An error occurred: " + ex.Message + "')</script>"); } } private void SaveToCSV() { try { string dateTime = DateTime.Now.ToString("yyyy-MM-dd"); string time = DateTime.Now.ToString("HH:mm:ss"); string tittle = "Medicine Master"; // Get additional details (you may customize this part) string details = $"New Medicine {medicine.Text} Added"; // Combine date, time, and details in CSV format with comma delimiter string contentToSave = $"\"{dateTime}\",\"{time}\",\"{tittle}\",\"{details}\""; // Specify the path to the CSV file (including the file name) string filePath = Server.MapPath("~/data/LogData.csv"); // Check if the directory exists, if not, create it string directoryPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } // Check if the file exists, if not, create and add headers if (!File.Exists(filePath)) { File.WriteAllText(filePath, "Date,Time,Tittle,Details" + Environment.NewLine); } // Save the content to the file File.AppendAllText(filePath, contentToSave + Environment.NewLine); } catch (Exception ex) { Response.Write("<script>alert('An error occurred while saving to CSV: " + ex.Message + "')</script>"); } } protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e) { Gridview1.EditIndex = e.NewEditIndex; BindGrid(); } protected void Gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { Gridview1.EditIndex = -1; // Exit edit mode BindGrid(); } protected void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e) { int rowIndex = e.RowIndex; GridViewRow row = Gridview1.Rows[rowIndex]; // Check if the row and controls exist if (row != null) { int id = Convert.ToInt32(Gridview1.DataKeys[rowIndex].Value); // Find the TextBox in the row TextBox txtMedicineName = row.FindControl("txtMedicineName") as TextBox; // Check if the TextBox is found if (txtMedicineName != null) { string updatedMedicineName = txtMedicineName.Text; // Connection string for your database string connectionString = WebConfigurationManager.ConnectionStrings["gav"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); // Define your SQL update query string updateQuery = "UPDATE medicine SET Medicine_name = @MedicineName WHERE ID = @ID"; using (SqlCommand cmd = new SqlCommand(updateQuery, conn)) { cmd.Parameters.AddWithValue("@MedicineName", updatedMedicineName); cmd.Parameters.AddWithValue("@ID", id); int rowsAffected = cmd.ExecuteNonQuery(); if (rowsAffected > 0) { Response.Write("<script>alert('Update Successfully')</script>"); } else { // Update failed // You can add an error message here } } } } else { // Handle the case where the TextBox is not found // You can add an error message or other handling here } } else { // Handle the case where the row is not found // You can add an error message or other handling here } // After updating, you can set EditIndex to -1 to exit edit mode Gridview1.EditIndex = -1; BindGrid(); // Rebind the data to the GridView } } }
| ver. 1.4 |
Github
|
.
| PHP 7.3.33 | Generation time: 0.04 |
proxy
|
phpinfo
|
Settings