Код: Выделить всё
Add Branch
×
Name:
Code:
Cancel
Код: Выделить всё
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string connectionString = "Data Source=MSI\\SQLEXPRESS;Initial Catalog=EKyc;Integrated Security=true"; // Replace with your actual connection string
using (SqlConnection con = new SqlConnection(connectionString))
{
// Open the connection
con.Open();
string nm = Brnm.Text.Trim();
string cd = Brcd.Text.Trim();
// Check if connection is open
if (con.State == ConnectionState.Open)
{
// Create SQL command with parameters
SqlCommand cmd = new SqlCommand("insert into Branche (Name, Code) VALUES (@Name,@Code)", con);
cmd.Parameters.AddWithValue("@Name", Brnm.Text.Trim()); // Assuming Brnm is the TextBox for Name
cmd.Parameters.AddWithValue("@Code", Brcd.Text.Trim()); // Assuming Brcd is the TextBox for Code
// Execute the command
int rowsAffected = cmd.ExecuteNonQuery();
// Check if insertion was successful
if (rowsAffected > 0)
{
// Insertion successful, show success message
Response.Write("alert('Branch added successfully " + nm + " ');");
// Optionally redirect to another page or reload current page
// Response.Redirect("BranchList.aspx"); // Example redirect
}
else
{
// No rows affected, handle error
Response.Write("alert('Error: No rows affected.');");
}
}
else
{
// Connection not opened
Response.Write("alert('Error: Database connection not open.');");
}
}
}
catch (Exception ex)
{
// Handle exceptions
Response.Write("alert('Error: " + ex.Message + "');");
// Optionally log the exception for further investigation
Console.WriteLine("Exception occurred: " + ex.Message);
}
}
Код: Выделить всё
введите сюда описание изображения
Почему это не работает? Что-то не так со свойством .Text текстового поля?
Подробнее здесь: https://stackoverflow.com/questions/786 ... lank-row-i