I am a beginner and I am creating a crud webpage in asp.net and i can't get the update(save) button working. here is the snippet of my code:
Save and Cancel buttons in Main.aspx
<div height: 334px;" class="gridview">
<asp:GridView ID="RetailInfoGridView"
AutoGenerateColumns="False"
ShowFooter="true"
DataKeyNames="StockKeepingID"
runat="server"
ShowHeaderWhenEmpty="True"
HeaderStyle-ForeColor="White"
AlternatingRowStyle="alt"
EmptyDataText="No Records Found"
AllowSorting="True"
OnRowCommand="RetailInfoGridView_RowCommand"
OnDataBound="RetailInfoGridView_DataBound"
OnRowDataBound="RetailInfoGridView_RowDataBound"
OnRowEditing="RetailInfoGridView_RowEditing"
OnRowCancelingEdit="RetailInfoGridView_RowCancelingEdit"
OnRowUpdating="RetailInfoGridView_RowUpdating"
onRowDeleting="RetailInfoGridView_RowDeleting"
OnSelectedIndexChanged="RetailInfoGridView_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Edit" CommandName="Edit" runat="server" Text="Edit" ToolTip="Edit" />
<asp:Button ID="Delete" CommandName="Delete" runat="server" Text="Delete" ToolTip="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Save" CommandName="Save" runat="server" Text="Save" ToolTip="Save" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="Add" CommandName="Add" runat="server" Text="Add" ToolTip="Add" />
</FooterTemplate>
</asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="Save" CommandName="Save" runat="server" Text="Save" ToolTip="Save" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
...
Main.aspx.cs
string connectionString = "Data Source=102000-LSU-2216;Initial Catalog=loginDB;Integrated Security=True";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateGridView();
}
}
protected void SearchTextBox_TextChanged(object sender, EventArgs e)
{
/* connect.Open();
string query = "SELECT * FROM RetailInfo WHERE StockKeepingUnit LIKE '%" + SearchTextBox.Text + "%'";
adapter = new SqlDataAdapter(query, connect);
table = new DataTable();
adapter.Fill(table);
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
connect.Close();*/
}
protected void SearchButton_Click(object sender, EventArgs e)
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM RetailInfo WHERE StockKeepingUnit LIKE '%" + SearchTextBox.Text + "%'", connect);
DataTable table = new DataTable();
adapter.Fill(table);
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
}
}
protected void RetailInfoGridView_DataBound(object sender, EventArgs e)
{
}
protected void RetailInfoGridView_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void RetailInfoGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
e.Row.ToolTip = "Click the link to view Description";
//e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(RetailInfoGridView, "Select$" + e.Row.RowIndex);
}
}
void PopulateGridView()
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM RetailInfo", connect);
DataTable table = new DataTable();
adapter.Fill(table);
if (table.Rows.Count > 0)
{
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
}
else
{
table.Rows.Add(table.NewRow());
RetailInfoGridView.DataSource = table;
RetailInfoGridView.DataBind();
RetailInfoGridView.Rows[0].Cells.Clear();
RetailInfoGridView.Rows[0].Cells.Add(new TableCell());
RetailInfoGridView.Rows[0].Cells[0].ColumnSpan = table.Columns.Count;
RetailInfoGridView.Rows[0].Cells[0].Text = "No record Found";
}
}
}
protected void RetailInfoGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("Add"))
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
string query = "INSERT INTO RetailInfo(StockKeepingUnit,UniversalProductCode,VendorName,ProductName,ProductDesc,RetailPrice) VALUES (@StockKeepingUnit,@UniversalProductCode,@VendorName,@ProductName,@ProductDesc,@RetailPrice)";
SqlCommand command = new SqlCommand(query, connect);
command.Parameters.AddWithValue("@StockKeepingUnit", (RetailInfoGridView.FooterRow.FindControl("skuTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@UniversalProductCode", (RetailInfoGridView.FooterRow.FindControl("upcTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@VendorName", (RetailInfoGridView.FooterRow.FindControl("vnTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductName", (RetailInfoGridView.FooterRow.FindControl("pnTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductDesc", (RetailInfoGridView.FooterRow.FindControl("pdTextBoxFooter") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@RetailPrice", (RetailInfoGridView.FooterRow.FindControl("rpTextBoxFooter") as TextBox).Text.Trim());
command.ExecuteNonQuery();
PopulateGridView();
ScriptManager.RegisterStartupScript(this, typeof(string), "Alert", "alert('New Record Added');", true);
}
}
}
catch (Exception ex)
{
}
}
protected void RetailInfoGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
RetailInfoGridView.EditIndex = e.NewEditIndex;
PopulateGridView();
}
protected void RetailInfoGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
RetailInfoGridView.EditIndex = -1;
PopulateGridView();
}
protected void RetailInfoGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
string query = "UPDATE RetailInfo SET StockKeepingUnit=@StockKeepingUnit,UniversalProductCode=@UniversalProductCode,VendorName=@VendorName,ProductName=@ProductName,ProductDesc=@ProductDesc,RetailPrice=@RetailPrice WHERE StockKeepingID=@id";
SqlCommand command = new SqlCommand(query, connect);
command.Parameters.AddWithValue("@StockKeepingUnit", (RetailInfoGridView.Rows[e.RowIndex].FindControl("skuTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@UniversalProductCode", (RetailInfoGridView.Rows[e.RowIndex].FindControl("upcTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@VendorName", (RetailInfoGridView.Rows[e.RowIndex].FindControl("vnTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductName", (RetailInfoGridView.Rows[e.RowIndex].FindControl("pnTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@ProductDesc", (RetailInfoGridView.Rows[e.RowIndex].FindControl("pdTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@RetailPrice", (RetailInfoGridView.Rows[e.RowIndex].FindControl("rpTextBox") as TextBox).Text.Trim());
command.Parameters.AddWithValue("@id", Convert.ToInt32(RetailInfoGridView.DataKeys[e.RowIndex].Value.ToString()));
command.ExecuteNonQuery();
RetailInfoGridView.EditIndex = -1;
PopulateGridView();
ScriptManager.RegisterStartupScript(this, typeof(string), "Alert", "alert('Record Updated');", true);
}
}
catch (Exception ex)
{
}
}
protected void RetailInfoGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
using (SqlConnection connect = new SqlConnection(connectionString))
{
connect.Open();
string query = "DELETE FROM RetailInfo WHERE StockKeepingID = @id";
SqlCommand command = new SqlCommand(query, connect);
command.Parameters.AddWithValue("@id", Convert.ToInt32(RetailInfoGridView.DataKeys[e.RowIndex].Value.ToString()));
command.ExecuteNonQuery();
PopulateGridView();
ScriptManager.RegisterStartupScript(this, typeof(string), "Alert", "alert('Record Deleted');", true);
}
}
catch (Exception ex)
{
}
}
Whenever I run the program, I edit values to a specific row and then I click the save button, it does not do anything. I have read other answers that said to have a !postback and DataKeyNames in the code but I already have the postback in Main.aspx.cs and the DataKeyNames on Main.aspx. I don't know if the error comes on the .aspx or on the .aspx.cs. Have I done something wrong on the code? any help would be highly appreciated. Thank you.
UPDATE:
I have solved the problem. instead of writing "save" variables:
<EditItemTemplate>
<asp:Button ID="Save" CommandName="Save" runat="server" Text="Save" ToolTip="Save" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
I changed it to "update":
<EditItemTemplate>
<asp:Button ID="Update" CommandName="Update" runat="server" Text="Update" ToolTip="Update" />
<asp:Button ID="Cancel" CommandName="Cancel" runat="server" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
Although I do not know why it worked, but it did the job!
If you know why "save" did not work and and "update" worked, I would really appreciate it if you tell me so i can be enlightened.
Thank you for all who helped.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.