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
 Display Data in grid view upon insert and edit it
Posted by becky bloomwood on  2/4/2011 9:40:46 AM Category: ADO.NET
Add to my favorites
Email to friend
Hi all,
currently I am doing an online store project using an example from one of your tutorials. In the grid view is populated with data and at the below of the grid view, there is a row of empty fields populated below. After user have enter all the necessary info and clicks on the Add New link, the data will be inserted into the database and it will also be displayed in the grid view itself. User is also able to edit and update the records.

The software that I am using is called: MS VS 2008 C#
The database that I am using is called: MS SQL Server 2008

This is the database.cs for inserting records into the database:
public bool Insert_ItemsRecords(string id, string itemName, string itemDesc)
{
SqlCommand cmd_ItemsList = new SqlCommand();

cmd_ItemsList.CommandText = "[VRM].[INSERT_ItemsRecords]";
cmd_ItemsList.CommandType = CommandType.StoredProcedure;
cmd_ItemsList.Parameters.Clear();

SqlParameter sqlParaID = new SqlParameter("@id", SqlDbType.VarChar, 10);
sqlParaID.Value = id;
cmd_ItemsList.Parameters.Add(sqlParaID);

SqlParameter sqlParaItemName = new SqlParameter("@itemName", SqlDbType.VarChar, 100);
sqlParaItemName.Value = itemName;
cmd_ItemsList.Parameters.Add(sqlParaItemName);

SqlParameter sqlParaItemDesc = new SqlParameter("@itemDesc", SqlDbType.VarChar, 1000);
sqlParaItemDesc.Value = itemDesc;
cmd_ItemsList.Parameters.Add(sqlParaItemDesc);

return executeNotQuery(cmd_ItemsList);
}
This is the stored procedure:
ALTER PROCEDURE [OS].[INSERT_ItemsRecords]
@id varchar(10),
@itemName varchar(100),
@itemDesc varchar(1000)


AS
INSERT INTO OS.Items (id, itemName, itemDesc)
VALUES (@id, @itemName, @itemDesc)

This is the business logic for inserting records:
protected void gvItems_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtID = (TextBox)gvItems.FooterRow.FindControl("txtID");
TextBox txtItemName = (TextBox)gvItems.FooterRow.FindControl("txtItemName");
TextBox txtItemDesc= (TextBox)gvItems.FooterRow.FindControl("txtItemDesc");



osdb.Insert_ItemsRecords(txtID.Text, txtItemName.Text, ttxtItemDesc.Text);
BindGrid();
}
}

Questions: When I enter in the necessay info and clicks on the Add New, the data will not be displayed in the grid view. It is only displayed once I close it and debug it again. Also, it insert the records into the database twice instead of once.

Next if user enters in the records wrongly and clicks on the edit link, they can edit and update it:
This is the database.cs for editing and update of records:
public bool Update_ItemsRecords(string id, string itemName, string itemDesc)
{
SqlCommand cmd_ItemsList = new SqlCommand();

cmd_ItemsList.CommandText = "[Common].[UPDATE_ItemsRecords]";
cmd_ItemsList.CommandType = CommandType.StoredProcedure;
cmd_ItemsList.Parameters.Clear();

SqlParameter sqlParaID = new SqlParameter("@id", SqlDbType.VarChar, 10);
sqlParaID.Value = id;
cmd_ItemsList.Parameters.Add(sqlParaID);

SqlParameter sqlParaItemName = new SqlParameter("@itemName", SqlDbType.VarChar, 100);
sqlParaItemName.Value = itemName;
cmd_ItemsList.Parameters.Add(sqlParaItemName);

SqlParameter sqlParaItemDesc = new SqlParameter("@itemDesc", SqlDbType.VarChar, 1000);
sqlParaItemDesc.Value = itemDesc;
cmd_ItemsList.Parameters.Add(sqlParaItemDesc);

return executeNotQuery(cmd_ItemsList);
}

This is the stored procedure for editing and updating of records:
ALTER PROCEDURE [OS].[UPDATE_ItemsRecords]
@id varchar(10),
@itemName varchar(100),
@itemDesc varchar(1000)

AS
Select @id = id , @itemName = itemName, @itemDesc = itemDesc from OS.Items WHERE tid = @id
UPDATE OS.Items SET id = @id , itemName = @itemName, itemDesc = @itemDesc WHERE id = @id

This is the business logic for editing and updating of records:
protected void gvItems_RowEditing(object sender, GridViewEditEventArgs e)
{
gvItems.EditIndex = e.NewEditIndex;
BindGrid();
}




protected void gvItems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtItemID = (TextBox)gvItems.Rows[e.RowIndex].FindControl("txtTerms");
TextBox txtItemName = (TextBox)gvItems.Rows[e.RowIndex].FindControl("txtItemName");
TextBox txtItemDesc = (TextBox)gvItems.Rows[e.RowIndex].FindControl("txtItemDesc");

osdb.Update_ItemRecords(txtItemID.Text, txtItemName.Text, txtItemDesc.Text);

gvItems.EditIndex = -1;
BindGrid();
}

Questions is: When I click on edit for a particular record, by right the fields shld be populated from the data entered previouslt. But, it is empty and when I click on the update button, there is error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Thanks and sorry for the engthy post.


Post Your Reply Post New Question

 Viewer's Reply
RE: Display Data in grid view upon insert and edit it
Answered By Moderator1 on 2/15/2011 8:22:50 AM
Hi,
I think there is some problem in the BindGrid method.. Why you haven't posted that method?
 
Call bidGrid Method
Answered By self innovator on 12/22/2011 1:23:56 AM
When submitting new records or editing an existing records call the BINDGRID() method in both events.. it will display the records
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Oracle Magazine
The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks - Free 156 Page Preview
The Beginner's Guide to Joomla