...

View Full Version : Insert new records into the database



becky.b
02-01-2011, 06:44 AM
Hi, currently I am doing a web application project using 3-tier formating to do out the project. In the grid view is displayed with data retrieved from the database and at the footer there is a row of empty fields for the user to insert records into the database and when user clicks on Add New, a new row of empty fields will be populated below.

The database.cs and stored procedure is used to stored the data into the database.
This is the database.cs code:
public bool Insert_ItemRecords(string itemID, string itemName, string itemDesc)
{

SqlCommand cmd_ItemList = new SqlCommand(); cmd_ItemList.CommandText = "[VRM].[INSERT_ItemRecords]"; cmd_ItemList.CommandType = CommandType.StoredProcedure; cmd_ItemList.Parameters.Clear();
SqlParameter sqlParaItemID = new SqlParameter("@itemID", SqlDbType.VarChar, 10);
sqlParaItemID.Value = itemID;
cmd_ItemList.Parameters.Add(sqlParaItemID);
SqlParameter sqlParaItemName = new SqlParameter("@itemName", SqlDbType.VarChar, 100);
sqlParaItemName.Value = itemName;
cmd_ItemList.Parameters.Add(sqlParaItemName);
SqlParameter sqlParaItemDesc = new SqlParameter("@itemDesc", SqlDbType.VarChar, 1000);
sqlParaItemDesc.Value = fileName;
cmd_ItemList.Parameters.Add(sqlParaItemDesc);
return executeNotQuery(cmd_ItemList); }

This is the stored procedure code:
ALTER PROCEDURE [OS].[INSERT_ITEMRecords] @itemID varchar(10),@itemName varchar(100),@itemDesc varchar(1000) ASINSERT INTO OS.Items (itemID, itemName, itemDesc)VALUES (@itemID, @itemName, @itemDesc)

I am not sure of how to code out for the business logic?
I code this in business logic but its wrong as the error states the Insert method could not be found:
protected void gvCriticalTerms_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName.Equals("AddNew"))
{
TextBox txtItemID = (TextBox)gvItems.FooterRow.FindControl("txtItemID");
TextBox txtItemName = (TextBox)gvItems.FooterRow.FindControl("txtItemName");
TextBox txtItemDesc = (TextBox)gvItems.FooterRow.FindControl("txtItemDesc");

os.Insert_ItemsRecords(txtItemID .Text, txtItemName.Text, txtItemDesc.Text);
BindGrid1();
}
}

Thanks:D

peterC
02-24-2011, 08:46 AM
Can you insert normal Sql Statement ?
stirng str="INSERT INTO t1(id,name) values(1,'abc')

Then

Use ....Command for Insert Data



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum