hi,
i have used gridview in asp.net 2005 wit c#.i need to update my datas.in tat i hv placed dropdownlist,checkbox controls.but its not updating the datas.when i edit the row,we have dropdownlist but not tat database value.so when i edit the row the database value should be in dropdown list.when i click update link i got this error.The GridView 'GridView1' fired event RowUpdating which wasn't handled.so pls help me to do this.
this is coding
-----------------

protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {          GridViewRow gv = null;
            string k = string.Format(GridView1.Rows[gv.RowIndex].Cells[4].Text);
            Response.Write(k);
            TextBox t1, t2, t3, t4, t5,t6,t7,t8,t9,t10,t11;
            DropDownList d1, d2, d3, d4,d5;
            CheckBox cdr, cdrw, cdvdr, cdvdrw, cfdd, cinter, cwa;
            t1 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[5].Controls[1];
            t2 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[6].Controls[1];
            t3 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[7].Controls[1];
            t4 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[8].Controls[1];           
            t5 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[9].Controls[1];
            t6 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[10].Controls[1];
            t7 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[11].Controls[1];
            t8 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[18].Controls[1];
            t9 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[20].Controls[1];
            t10 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[21].Controls[1];
            t11 = (TextBox)GridView1.Rows[gv.RowIndex].Cells[22].Controls[1];       
                d1 = (DropDownList)GridView1.Rows[gv.RowIndex].Cells[0].Controls[0];
                d2 = (DropDownList)GridView1.Rows[gv.RowIndex].Cells[1].Controls[0];
                d3 = (DropDownList)GridView1.Rows[gv.RowIndex].Cells[2].Controls[0];
                d4 = (DropDownList)GridView1.Rows[gv.RowIndex].Cells[3].Controls[0];
                d5 = (DropDownList)GridView1.Rows[gv.RowIndex].Cells[23].Controls[0];
                    cdr =  (CheckBox)GridView1.Rows[gv.RowIndex].Cells[12].Controls[2];
                    cdrw = (CheckBox)GridView1.Rows[gv.RowIndex].Cells[13].Controls[2];
                    cdvdr =(CheckBox)GridView1.Rows[gv.RowIndex].Cells[14].Controls[2];                    cdvdrw=(CheckBox)GridView1.Rows[gv.RowIndex].Cells[15].Controls[2];
                    cfdd = (CheckBox)GridView1.Rows[gv.RowIndex].Cells[16].Controls[2];
                    cinte =(CheckBox)GridView1.Rows[gv.RowIndex].Cells[17].Controls[2];
                    cwa =  (CheckBox)GridView1.Rows[gv.RowIndex].Cells[19].Controls[2];
            int cd_r = 0, cd_rw = 0, dvd_r = 0, dvd_rw = 0, fdd = 0, inter = 0, wa = 0;
            if (cdr.Checked)
            {                cd_r = 1;            }
            if (cdrw.Checked==true)
            {                cd_rw = 1;            }
            if (cdvdr.Checked==true)
            {                dvd_r = 1;            }
            if (cdvdrw.Checked == true)
            {                dvd_rw = 1;            }
            if (cfdd.Checked == true)
            {                fdd = 1;            }
            if (cinter.Checked == true)
            {                inter = 1;            }
            if (cwa.Checked == true)
            {                wa = 1;            }      
        string upd="update reg_laptop set block='"+d1.SelectedValue+"',location='"+d2.SelectedValue+"',department='"+d3.SelectedValue +"',pcname='"+d4.SelectedValue+"',os='"+t1.Text+"',procmodel='"+t2.Text +"',procspeed='"+t3.Text+"',ram='"+t4.Text+"',harddisk='"+t5.Text+"',cpuno='"+t6.Text +"',monino='"+t7.Text +"',cd_r="+cd_r+",cd_rw="+cd_rw+",dvd_r="+dvd_r +",dvd_rw="+dvd_rw+",fdd="+fdd+",internetconnection="+inter+",dateofpurchase='"+t8.Text+"',waranty_amc="+wa+",frmdate='8/5/2003',todate='8/5/2009',compname='"+d5.SelectedValue+"',contactperson='"+t9.Text+"' where ipaddress='"+k+"'";
        cmd = new SqlCommand(upd, con);
        cmd.ExecuteNonQuery();
        Response.Write("ur datas r updated");
        GridView1.EditIndex = -1;
        binddata();         }

===========================================================

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)    {       
        GridView1.EditIndex = e.NewEditIndex;
        binddata();    }

thank you

Recommended Answers

All 15 Replies

hi,
can u try to write that whole code in row updating instead of rowupdated.

:) tring the code in rowupdating event

ya now i tried in updating event. its working.but again i have error .
in this line..
GridViewRow gv = null;
string k = string.Format(GridView1.Rows[gv.RowIndex].Cells[4].Text);

bec i have created gv for gridviewrow... and assigned null.
If i don't give gv =null it show an error
so pls give suggestion for rectify this error.

hi ,
instead of gv i use e.so i rectified tat error.
but one more error is
cdr = (CheckBox)GridView1.Rows[gv.RowIndex].Cells[12].Controls[2];
because in database i used bit value.tat is 0 or 1.and if it is in grid view then it shows No or Yes. when i edit tat it shows chek box .
so when i check the check box 1 should be stored in database
so pls help me to do this

thanking you

hey priya,
try like this
checkbox cb=(checkbox)gridview1.findcontrol("checkbox1");
if(cb.checked)
{
//so so
}
else
{
//so so
}
may it works...

Note:iam not intrested in case sensitivity...use proper cases as c# is case sensitive....

ya. i gave like this as you told.
cdr = (CheckBox)GridView1.FindControl("chk_cdr");
but its showing an error.when i trace it value cdr = null
and i got this error Object reference not set to an instance of an object
how can i get the 1 when i check the checkbox

i tried in checkedchanged event..tat event also is not working. shows the same error

thank you sir,

hey priya,
can u ones post the .aspx page code...i will check 1s so that i will get some idea....

Ya , this is aspx code sir,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="reg_editlaptop.aspx.cs" Inherits="reg_editlaptop" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None"
Height="219px" Width="1367px" AllowSorting="True" EnableTheming="True" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating">
<FooterStyle BackColor="Tan" />
<Columns>
<asp:TemplateField HeaderText="Block">
<EditItemTemplate>
&nbsp;
<asp:DropDownList ID="ddl_block" runat="server" AutoPostBack="True" Width="130px" DataTextField ="descriptions" DataValueField ="sid" DataSource="<%#loadblock()%>" AppendDataBoundItems="True" OnSelectedIndexChanged="ddl_block_SelectedIndexChanged">
<asp:ListItem>--------Select--------</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("block") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<EditItemTemplate>
&nbsp;<asp:DropDownList ID="ddl_loc" runat="server" DataSource ="<%#loadloc()%>" DataTextField ="descriptions" DataValueField ="sid" AppendDataBoundItems="True" AutoPostBack="True"
Width="130px">
<asp:ListItem>--------Select--------</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("location") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<EditItemTemplate>
&nbsp;<asp:DropDownList ID="ddl_dep" runat="server" DataSource ="<%#loaddep()%>" DataTextField="descriptions" DataValueField ="sid" AppendDataBoundItems="True" AutoPostBack="True"
Width="130px">
<asp:ListItem>--------Select--------</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("department") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sys-Name">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" DataSource="<%#loadpc()%>" DataTextField="descriptions" DataValueField="sid" AutoPostBack="True" Width="130px">
<asp:ListItem>--------Select--------</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("pcname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ipaddress" HeaderText="IP-Address" ReadOnly="True" />
<asp:BoundField DataField="os" HeaderText="OS" />
<asp:BoundField DataField="procmodel" HeaderText="Processor Model" />
<asp:BoundField DataField="procspeed" HeaderText="Processor Speed" />
<asp:BoundField DataField="ram" HeaderText="Ram" />
<asp:BoundField DataField="harddisk" HeaderText="Hard Disk" />
<asp:BoundField DataField="cpuno" HeaderText="Cpu No." />
<asp:BoundField DataField="monino" HeaderText="Monitor No." />
<asp:TemplateField HeaderText="CD-R">
<EditItemTemplate>
<asp:CheckBox ID="chk_cdr" runat="server" OnCheckedChanged="chk_cdr_CheckedChanged" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("cd_r") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CD_RW">
<EditItemTemplate>
<asp:CheckBox ID="chk_cdrw" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("cd_rw") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DVD_R">
<EditItemTemplate>
<asp:CheckBox ID="chk_dvdr" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("dvd_r") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DVD_RW">
<EditItemTemplate>
<asp:CheckBox ID="chk_dvdrw" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%# Bind("dvd_rw") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FDD">
<EditItemTemplate>
<asp:CheckBox ID="chk_fdd" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("fdd") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Internet">
<EditItemTemplate>
<asp:CheckBox ID="chk_ineter" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("internetconnection") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="dateofpurchase" HeaderText="DOP" />
<asp:TemplateField HeaderText="W/A">
<EditItemTemplate>
<asp:CheckBox ID="chk_wa" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("wa") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="frmdate" HeaderText="From Date" />
<asp:BoundField DataField="todate" HeaderText="To Date" />
<asp:TemplateField HeaderText="Company Name">
<EditItemTemplate>
<asp:DropDownList ID="ddl_compname" runat="server" AutoPostBack="True" Width="123px">
<asp:ListItem>---------Select--------</asp:ListItem>
<asp:ListItem>Hcl</asp:ListItem>
<asp:ListItem>Accel</asp:ListItem>
<asp:ListItem>MasterMind</asp:ListItem>
<asp:ListItem>Dell</asp:ListItem>
<asp:ListItem>logitech</asp:ListItem>
<asp:ListItem>Samsung</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("compname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="contactperson" HeaderText="Contact Person" />
<asp:CommandField ShowEditButton="True" />
</Columns>
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
&nbsp;<br />

</div>
</form>

</body>
</html>
thanking you,

hi sir,
Hve u found the ans
if so pls inform me as soon as possible.
thanking you,

hey,
everything is fine.in that code....in edit mode did u get the checkboxes in interface...did u write ur code in rowupdating event only..did u declare cdr as checkbox object globally...?check these things......

hey,
everything is fine.in that code....in edit mode did u get the checkboxes in interface...did u write ur code in rowupdating event only..did u declare cdr as checkbox object globally...?check these things......

ya i hav writen al as u mentioned.the problem is
cdrw = (CheckBox)GridView1.FindControl("chk_cdrw");
here the check box chk_cdrw value doesnt set for the varibale cdrw.. it shows null value when i trace the codings
and how to get the checkbox value bec it doesnt know how to get value whether the check box is checked or not
so if u knw just infrm me

Thank you so much for helping til now...

hey priya,
first dont say sir.
second to get checkbox value first u need to get the checkbox reference that ur not getting.(u r getting null there).ones u have reference to that control ..then u can check the value simply by...

cdr.Checked property..which returns boolean value..if checked it returns true..otherwise false....

hope its useful.....

hey priya,
first dont say sir.
second to get checkbox value first u need to get the checkbox reference that ur not getting.(u r getting null there).ones u have reference to that control ..then u can check the value simply by...

cdr.Checked property..which returns boolean value..if checked it returns true..otherwise false....

hope its useful.....

cdr = new CheckBox();
cdr = ((CheckBox)GridView1.FindControl("chk_cdr"));
hey like that is wat i created the refre for cdr. i do no whet its right so can u help me how to create

cdr = new CheckBox();
cdr = ((CheckBox)GridView1.FindControl("chk_cdr"));
hey like that is wat i created the refre for cdr. i do no whet its right so can u help me how to create

I got the answer properly.its updating nicely. thanks a lot
but when i edit the gridview , if the colum has yes value then the check box shud be cheked ...how to do tat...

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.