AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 ASP.NET 2.0 simple insert,edit,delete
Posted by priya binu on  7/25/2007 12:45:24 AM Category: C#
Add to my favorites
Email to friend
Hi,
I was trying the simple insert,edit,delete in gridview as in your article. pls help me how to write insert,update statements inside a class file ?

thanx in advance

Post Your Reply Post New Question

 Viewer's Reply
my answer
Answered By ramesh babu on 7/26/2007 12:22:05 AM
public void Insert(int empno, string empname, string designation, string salary)
{
SqlCommand cmd = new SqlCommand("sp_AddEmpDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@EmpNo", SqlDbType.Int, 4, "EmpNo");
SqlParameter p2 = new SqlParameter("@EmpName", SqlDbType.VarChar, 50, "EmpName");
SqlParameter p3 = new SqlParameter("@Designation", SqlDbType.VarChar, 50, "Designation");
SqlParameter p4 = new SqlParameter("@Salary", SqlDbType.VarChar, 50, "Salary");
p1.Value = empno;
p2.Value = empname;
p3.Value = designation;
p4.Value = salary;
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// Write your own Insert statement blocks
}

public DataTable Fetch()
{
SqlDataAdapter da = new SqlDataAdapter("select * from EmpTest", con);
DataSet ds = new DataSet();

da.Fill(ds, "emp");
DataTable dt = new DataTable();
dt = ds.Tables[0];
return dt;

// Write your own Fetch statement blocks, this method should return a DataTable
}



public void Update(int empno, string empname, string designation, string salary)
{
SqlCommand cmd = new SqlCommand("sp_EditEmpDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@EmpNo", SqlDbType.Int, 4, "EmpNo");
SqlParameter p2 = new SqlParameter("@EmpName", SqlDbType.VarChar, 50, "EmpName");
SqlParameter p3 = new SqlParameter("@Designation", SqlDbType.VarChar, 50, "Designation");
SqlParameter p4 = new SqlParameter("@Salary", SqlDbType.VarChar, 50, "Salary");
p1.Value = empno;
p2.Value = empname;
p3.Value = designation;
p4.Value = salary;
cmd.Parameters.Add(p1);
cmd.Parameters.Add(p2);
cmd.Parameters.Add(p3);
cmd.Parameters.Add(p4);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

public void Delete(int empno)
{
SqlCommand cmd = new SqlCommand("sp_DeleteEmpDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("@EmpNo", SqlDbType.Int, 4, "EmpNo");
p1.Value = empno;
cmd.Parameters.Add(p1);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
 
help again
Answered By priya binu on 7/26/2007 12:34:50 AM
Hi,
thanx for reply....but i want this to insert,edit,delete values from gridview like this
//Insert
private void SaveLogBookDetail()
{
dt = (DataTable)Session["Table"];
DataRow dr;
for (int i = 0; i < dt.Rows.Count; i++)
{

SqlParameter[] arParms = new SqlParameter[5];
dr = dt.Rows[i];
// @ErrorNo Return Parameter
arParms[0] = new SqlParameter("@ErrorNo", SqlDbType.Int);
arParms[0].Direction = ParameterDirection.ReturnValue;

arParms[1] = new SqlParameter("@Log_Id", SqlDbType.Int);
arParms[1].Value = Session["LogID"];

arParms[2] = new SqlParameter("@Log_Time", SqlDbType.DateTime);
arParms[2].Value = Convert.ToDateTime(dr[0].ToString(), System.Globalization.CultureInfo.GetCultureInfo("en-GB"));

arParms[3] = new SqlParameter("@Log_EventCode", SqlDbType.VarChar, 10);
arParms[3].Value = dr[1].ToString();

arParms[4] = new SqlParameter("@Create_Dt", SqlDbType.DateTime);
arParms[4].Value = System.DateTime.Now;


try
{
// Call ExecuteNonQuery static method of SqlHelper class
// We pass in database connection string, command type, stored procedure name and an array of SqlParameter objects
SqlHelper.ExecuteNonQuery(scon, CommandType.StoredProcedure, "sp_InsertLogBookDetail", arParms);
if (Convert.ToInt32(arParms[0].Value) == 99)
{

lblDisplay.Visible = true;
lblDisplay.Text = "Could not complete the operation successfully";


}

}
catch (Exception ex)
{

lblDisplay.Text = ex.Message;
}
}

}

here Log_time and event are text box,and combo box which comes from gridview in the form...
how to do this ,executing this insert ? similarly delete ,edit gridview...
thanx
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
The Beginner's Guide to Joomla
Programmers Heaven C# School Book - Free 338 Page eBook
The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition - Free 207 Page Preview!