Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a custom edit button for my first project. To summarize the contents of my table, the columns have data like this:

Type Name Image
Guitar Ibanez test1.jpg
Guitar Fender test2.jpg

I was successful to display the table along with the edit,update and cancel buttons. But when i try to change the name "Fender" to "Gibson", it is giving me this error:Cannot insert the value NULL into column 'type', table 'BrandsDB.dbo.guitarBrands'; column does not allow nulls. UPDATE fails. The statement has been terminated.

And the strange part is, when i ran the program again. It updates to finally what i wanted, but the error before that is the problem.

Type Name Image
Guitar Ibanez test1.jpg
Guitar Gibson test2.jpg

What I have tried:

Here is the code for aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"   Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body>
<form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="ButtonEdit" runat="server" Text="Edit" CommandName="Edit"/>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Button ID="ButtonUpdate" runat="server" Text="Update" CommandName="Update"/>
                    <asp:Button ID="ButtonCancel" runat="server" Text="Cancel" CommandName="Cancel"/>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Type">
                <ItemTemplate>
                    <asp:Label ID="LabelType" runat="server" Text='<%# Eval("Type") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txt_Type" runat="server" Text='<%# Eval("Type") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                   <asp:Label ID="LabelName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                </ItemTemplate>
            <EditItemTemplate>
        <asp:TextBox ID="txt_Name" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
    <ItemTemplate>
        <asp:Label ID="LabelImage" runat="server" Text='<%# Eval("Image") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txt_Image" runat="server" Text='<%# Eval("Image") %>'></asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>

        </Columns>
    </asp:GridView>

    <br/>
    <asp:Label ID="lblId" runat="server" Text="Label"></asp:Label><br/>
    <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label><br/>
</form>


C#
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;

 public partial class _Default : System.Web.UI.Page
 {

SqlConnection con1 = new SqlConnection(@"Data Source=Y560\SQLEXPRESS;Initial Catalog=BrandsDB;Integrated Security=True");
SqlCommand cmd, cmd1;
DataSet ds, ds1;
public _Default()
{

    cmd1 = new SqlCommand();
    ds1 = new DataSet();
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bindgridviewguitarbrands();
    }
}

//Start of Gridview Code for Guitar Brands
private void bindgridviewguitarbrands()
{

    con1.Open();
    cmd1.CommandText = "SELECT * FROM [guitarBrands]";
    cmd1.Connection = con1;
    SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
    da1.Fill(ds1);
    con1.Close();


    GridView1.DataBind();

}

protected void Button1_Click(object sender, EventArgs e)
{

        Button btn = sender as Button;
        GridViewRow gridrow = btn.NamingContainer as GridViewRow;
        int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
        string name = GridView1.Rows[gridrow.RowIndex].Cells[3].Text;

        lblId.Text = id.ToString();
        lblName.Text = name;

}

// row edit event
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    bindgridviewguitarbrands();

}
// row update event
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    // find student id of edit row
    string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

    // find updated values for update
    TextBox type = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Type");
    TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Name");
    TextBox image = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_Image");

    SqlCommand cmd1 = new SqlCommand("update guitarBrands set Type='" + type.Text + "', Name = '"+name.Text+"', Image = '"+image.Text+"' where ID = "+id, con1);
    con1.Open();
    cmd1.ExecuteNonQuery();
    con1.Close();

    lblName.Text = name.ToString();
    GridView1.EditIndex = -1;
    bindgridviewguitarbrands();
}
// cancel row edit event
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    bindgridviewguitarbrands();
}
 }
Posted
Comments
BebeSaiyan 24-Feb-17 2:37am    
okay thanks i'll look into it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900