[go: up one dir, main page]

0% found this document useful (0 votes)
6 views4 pages

Add Updat Delete Unit

The document is a C# ASP.NET web form that manages unit records in a database. It includes methods for adding, updating, and deleting units, as well as checking for existing unit codes to prevent duplicates. The code uses SQL commands to interact with a database and provides user feedback through alert messages on success or error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Add Updat Delete Unit

The document is a C# ASP.NET web form that manages unit records in a database. It includes methods for adding, updating, and deleting units, as well as checking for existing unit codes to prevent duplicates. The code uses SQL commands to interact with a database and provides user feedback through alert messages on success or error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication_Training
{
public partial class WebForm8 : System.Web.UI.Page
{
string constr =
ConfigurationManager.ConnectionStrings["con"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{

protected void Add_Unit(object sender, EventArgs e)


{
alreadyExistUnitCode();
}
bool addUnit()
{
string unit_name = txtUnitName.Text.Trim();
string unit_code = txtUnitCode.Text.Trim();

using (SqlConnection con = new SqlConnection(constr))


{
string query = "INSERT INTO unit(unit_name, unit_code) VALUES
(@unit_name,@unit_code)";

using (SqlCommand cmd = new SqlCommand(query, con))


{
cmd.Parameters.Add("@unit_name", SqlDbType.NVarChar).Value =
unit_name;
cmd.Parameters.Add("@unit_code", SqlDbType.NVarChar).Value =
unit_code;
try
{
con.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Successfully Added New
Unit!')</script>");
return true;

}
catch (Exception ex)
{
Response.Write("<script>alert('Error: " + ex.Message +
"')</script>");
return false;
}
}
}
}
bool alreadyExistUnitCode()
{
string unit_code = txtUnitCode.Text.Trim();

SqlConnection con = new SqlConnection(constr);


con.Open();
string query = "SELECT * FROM unit WHERE unit_code = @unit_code";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@unit_code", SqlDbType.NVarChar).Value = unit_code;
SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
Response.Write("<script>alert('This Unit Code is already exists!');
</script>");
return true;
}
else
{
return addUnit();
}
}
protected void Update_Unit(object sender, EventArgs e)
{
Response.Write("hello update");
//if (existUnitCode()) {
// updateUnit();
//}
}

bool existUnitCode()
{
string unit_code = txtUnitCode.Text.Trim();

SqlConnection con = new SqlConnection(constr);


con.Open();
string query = "SELECT * FROM unit WHERE unit_code = @unit_code";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@unit_code", SqlDbType.NVarChar).Value = unit_code;
SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
return true;
}
else
{
Response.Write("<script>alert('Unit Code is not
exists!');</script>");
return false;
}
}
void updateUnit()
{
string unit_name = txtUnitName.Text.Trim();
string unit_code = txtUnitCode.Text.Trim();
using (SqlConnection con = new SqlConnection(constr))
{
string query = "UPDATE unit SET unit_name = @unit_name WHERE
unit_code = @unit_code";

using (SqlCommand cmd = new SqlCommand(query, con))


{
cmd.Parameters.Add("@unit_name", SqlDbType.NVarChar).Value =
unit_name;
cmd.Parameters.Add("@unit_code", SqlDbType.NVarChar).Value =
unit_code;
try
{
con.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Successfully Updated
Unit!');</script>");

}
catch (Exception ex)
{
Response.Write("<script>alert('Error: " + ex.Message +
"');</script>");

}
}
}
}

protected void Delete_Unit(object sender, EventArgs e)


{
Response.Write("hello delete");
//if (existUnitCode())
//{
// deleteUnit();
//}
}
void deleteUnit()
{
string unit_code = txtUnitCode.Text.Trim();

using (SqlConnection con = new SqlConnection(constr))


{
string query = "DELETE FROM unit WHERE unit_code = @unit_code";

using (SqlCommand cmd = new SqlCommand(query, con))


{
cmd.Parameters.Add("@unit_code", SqlDbType.NVarChar).Value =
unit_code;
try
{
con.Open();
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Successfully Delete
Unit!')</script>");

}
catch (Exception ex)
{
Response.Write("<script>alert('Error: " + ex.Message +
"')</script>");

}
}
}
}
}
}

You might also like