Hi guys,

I've recently developed a gridview with edit,delete and update panel.....
All is working fine except the Update button.....the error shown is......

[B]Object reference not set to an instance of an object.[/B]
Source Error:

Line 48:         GridView1.EditIndex = -1;
Line 49:         conn.Open();
Line 50:         SqlCommand cmd = new SqlCommand("update Info set Name=''" + Namet.Text + "'',Username=''" + Usernamet.Text + "'',Email=''" + Emailt.Text + "'',Contact=''" + Contactt.Text + "'',Address=''" + Addresst.Text + "'',Country=''" + Countryt.Text + "'',Password=''" + Passwordt.Text + "'' where UserID=" + UserIDl.Text + "", conn);
Line 51:         cmd.ExecuteNonQuery();
Line 52:         conn.Close();

It's showing error in the sqlcommand method (Line 50)

It's really urgent guys so please help me out of this....

My full codes are as follows.......

UpdateUsers.aspx

<%@ Page Language="C#" MasterPageFile="~/Admin-IMKM/KMAdmin.master" AutoEventWireup="true"
    CodeFile="UpdateUsers.aspx.cs" Inherits="Admin_IMKM_UpdateUsers" %>

<asp:Content ID="KMContent1" ContentPlaceHolderID="KMAdminCPH" runat="server">
    <table cellpadding="0" cellspacing="0" align="center">
        <tr>
            <td align="center">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="True"
                    AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" OnRowEditing="GridView1_RowEditing"
                    OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating"
                    OnRowDeleting="GridView1_RowDeleting" BackColor="#FF8080" BorderColor="SaddleBrown"
                    BorderStyle="None" CellPadding="4" ForeColor="#333333" GridLines="None" PageSize="5">
                    <Columns>
                        <asp:TemplateField HeaderText="User ID">
                            <ItemTemplate>
                                <asp:Label ID="UserIDl" runat="server" Text='<%#Eval("UserID")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name">
                            <ItemTemplate>
                                <%#Eval("Name")%></ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="EditNamet" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Username">
                            <ItemTemplate>
                                <%#Eval("Username")%></ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="EditUsernamet" runat="server" Text='<%#Eval("Username") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Email">
                            <ItemTemplate>
                                <%#Eval("Email")%></ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="EditEmailt" runat="server" Text='<%#Eval("Email") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Contact">
                            <ItemTemplate>
                                <%#Eval("Contact")%></ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="EditContactt" runat="server" Text='<%#Eval("Contact") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Address">
                            <ItemTemplate>
                                <%#Eval("Address")%></ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="EditAddresst" runat="server" Text='<%#Eval("Address") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Country">
                            <ItemTemplate>
                                <%#Eval("Country")%></ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="EditCountryt" runat="server" Text='<%#Eval("Country") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Password">
                            <ItemTemplate>
                                <%#Eval("Password")%></ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="EditPasswordt" runat="server" Text='<%#Eval("Password") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
            </td>
        </tr>
    </table>
</asp:Content>

UpdateUsers.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_IMKM_UpdateUsers : System.Web.UI.Page
{
    SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["KMCon"].ToString());
        if(!IsPostBack )
        {
            bind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
   
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label UserIDl = (Label)row.FindControl("UserID");
        TextBox Namet = (TextBox)row.FindControl("EditNamet");
        TextBox Usernamet= (TextBox)row.FindControl("EditUsernamet");
        TextBox Emailt= (TextBox)row.FindControl("EditEmailt");
        TextBox Contactt= (TextBox)row.FindControl("EditContactt");
        TextBox Addresst= (TextBox)row.FindControl("EditAddresst");
        TextBox Countryt= (TextBox)row.FindControl("EditCountryt");
        TextBox Passwordt= (TextBox)row.FindControl("EditPasswordt");
        GridView1.EditIndex = -1;
        conn.Open();
        SqlCommand cmd = new SqlCommand("update Info set Name=''" + Namet.Text + "'',Username=''" + Usernamet.Text + "'',Email=''" + Emailt.Text + "'',Contact=''" + Contactt.Text + "'',Address=''" + Addresst.Text + "'',Country=''" + Countryt.Text + "'',Password=''" + Passwordt.Text + "'' where UserID=" + UserIDl.Text + "", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
       
    }
    public void bind()
    {
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from Info", conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "Info");
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        Label UserIDl1 = (Label)row.FindControl("UserIDl");
        conn.Open();
        SqlCommand cmd = new SqlCommand("delete from Info where UserID=" + UserIDl1.Text + "", conn);
        cmd.ExecuteNonQuery();
        conn.Close();
        bind();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
}
kvprajapati commented: Please do not email or PM forum staff with your support questions. -2

hey dude -
Put this line GridView1.EditIndex = -1; after conn.Close() in RowUpdting event.

still facing the same prob.....

Label UserIDl = (Label)row.Cells[0].FindControl("UserID");
  TextBox Namet = (TextBox)row.Cells[1].FindControl("EditNamet");
commented: sorry it didn't worked..... +0

Oh i see... try to set the DataKeyNames="UserID" property in GridView..
then in the GridView_RowUpdating().. comment the below line
Label UserIDl = (Label)row.FindControl("UserID");
the reason to make the above line commented is that in the Edit mode, UserID label will not exist as this is defined in the ItemTemplate of grid view.

now in Where clause, use the below line
GridView1.DataKeys[e.RowIndex].Value.ToString()..

commented: thanks bro.... +1

Try This..

it contains the code for download..

commented: You're a real gem for me..... +1

Thanks dnanetwork......the last reply of yours worked.....You're indeed a genius....Thanks again...

This sound like this may be the solution to my problem also but the link to the final solution is not working.

For me edit and delete all appear to be working but the update always returns the first (undedited) values not the value of what it was changed to.

Any thoughts? I can post code, but I believe it is almost identical to the example shown.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.