i am developing a website using asp.net c#. i have one form to get branch name and branch code code is working without any error but when checked database table its adding a blank row insted of data i have given this is my add branch model with proper asp text boxes with id and name
<!-- Add Branch Modal -->
<div class="modal fade" id="addBranchModal" tabindex="-1" role="dialog" aria-labelledby="addBranchModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addBranchModalLabel">Add Branch</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- Add Branch Form -->
<div class="form-group">
<label for="branchName">Name:</label>
<asp:TextBox id="Brnm" runat="server" type="text" class="form-control" placeholder="Name" requaired="requaired" ></asp:TextBox>
</div>
<div class="form-group">
<label for="branchCode">Code:</label>
<asp:TextBox id="Brcd" runat="server" type="text" class="form-control" placeholder="Code" required="required"></asp:TextBox>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" onclick="cancelbtn()" data-dismiss="modal">Cancel</button>
<!-- <button type="button" class="btn btn-primary" onclick="saveBranch()">Save</button -->
<asp:Button ID="Button2" runat="server" Text="Save" class="btn btn-primary" OnClick="Button2_Click" UseSubmitBehavior="false"/>
</div>
</div>
</div>
</div>
and this is my backend c# code to get connected with database
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("<script>alert('Branch added successfully " + nm + " ');</script>");
// Optionally redirect to another page or reload current page
// Response.Redirect("BranchList.aspx"); // Example redirect
}
else
{
// No rows affected, handle error
Response.Write("<script>alert('Error: No rows affected.');</script>");
}
}
else
{
// Connection not opened
Response.Write("<script>alert('Error: Database connection not open.');</script>");
}
}
}
catch (Exception ex)
{
// Handle exceptions
Response.Write("<script>alert('Error: " + ex.Message + "');</script>");
// Optionally log the exception for further investigation
Console.WriteLine("Exception occurred: " + ex.Message);
}
}
and this is what i have on the top of my admin page
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="EKyc.WebForm1" EnableEventValidation="false" EnableViewState="true" %>
can please someone help
this is what i see in database tableenter image description here
why is this not working?? is anything wrong with .Text property of textbox?
Tushar Gidde is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.