But here, when i tried to use my Edit Button (on my gridview task i added you know) it gives an error.
That is the error;
"NullReferenceExeption was unhandled by user code.
Object reference not set to an instance of an object. "
And when i press calculate button, the total result is not going in to my database. How can i insert the result into my database and how can i able to edit it ?
I'm waiting you helps with four-eye.
Regards.
Thanks.
If you understand what you're doing, you're not learning anything.
Kuthay Gumus
Member
13 Points
116 Posts
Object reference not set to an instance of an object - Inserting Results to Database
Nov 29, 2012 06:47 AM|LINK
Hello everyone !
I'm on a project which named " Personal Tracking ". On this project i tried to do that with GridView ;
Name WorkingType Monday Tuesday ................................. Total ButtonField
Mike outsource 70 60 ................................. 130 Add
Kate outsource 50 90 ............................... 140 Add
I calculated the days in to the last row on my "GridView".
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) this.BindGrid(); } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.Add("PAZARTESI"); dt.Columns.Add("SALI"); dt.Columns.Add("CARSAMBA"); dt.Columns.Add("PERSEMBE"); dt.Columns.Add("CUMA"); dt.Columns.Add("CUMARTESI"); dt.Columns.Add("PAZAR"); dt.Rows.Add(new Object[] { 14, 15, 16, 17, 18, 19, 20 }); dt.Rows.Add(new Object[] { 24, 25, 26, 27, 28, 29, 30 }); dt.Rows.Add(new Object[] { 34, 35, 36, 37, 38, 39, 40 }); dt.Rows.Add(new Object[] { 44, 45, 46, 47, 48, 49, 50 }); dt.Rows.Add(new Object[] { 54, 55, 56, 57, 58, 59, 60 }); dt.Rows.Add(new Object[] { 64, 65, 66, 67, 68, 69, 70 }); dt.Rows.Add(new Object[] { 74, 75, 76, 77, 78, 79, 80 }); //this.GridView1.DataSource = dt; this.GridView1.DataBind(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = (e.CommandSource as Button).NamingContainer as GridViewRow; Label Label1 = row.FindControl("Label1") as Label; Label1.Text = (Convert.ToDouble(row.Cells[4].Text) + Convert.ToDouble(row.Cells[5].Text) + Convert.ToDouble(row.Cells[6].Text) + Convert.ToDouble(row.Cells[7].Text) + Convert.ToDouble(row.Cells[8].Text) + Convert.ToDouble(row.Cells[9].Text) + Convert.ToDouble(row.Cells[10].Text)).ToString(); } }But here, when i tried to use my Edit Button (on my gridview task i added you know) it gives an error.
That is the error;
"NullReferenceExeption was unhandled by user code.
Object reference not set to an instance of an object. "
And when i press calculate button, the total result is not going in to my database. How can i insert the result into my database and how can i able to edit it ?
I'm waiting you helps with four-eye.
Regards.
Thanks.
ignatandrei
All-Star
135184 Points
21682 Posts
Moderator
MVP
Re: Object reference not set to an instance of an object - Inserting Results to Database
Nov 29, 2012 07:10 AM|LINK
maybe total is not null, because does not found.
Kuthay Gumus
Member
13 Points
116 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Nov 29, 2012 07:58 AM|LINK
i did not understand my friend.
Sagar Kamate
Participant
764 Points
162 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Nov 29, 2012 08:12 AM|LINK
on which line you getting an error?
I think you getting the null value from gridview row cells text.
So check wheather it is null before converting to double.
Ex: Convert.ToDouble(string.IsNullOrEmpty(row.Cells[4].Text) ? "0" : row.Cells[4].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[5].Text) ? "0" : row.Cells[5].Text) +.................so on.
Sagar Kamate, Software Engineer.
sagark_1008@yahoo.co.in
Don't forget to Mark this thread as "Answer", if it was useful to you.
sarathi125
Star
13599 Points
2691 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Nov 29, 2012 08:12 AM|LINK
Hi,
Try like the following
Label Total = (Label)row.FindControl("Total");
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Nov 30, 2012 06:27 AM|LINK
Hello
If possible, please show us your full aspx codes.
Kuthay Gumus
Member
13 Points
116 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Dec 07, 2012 08:28 AM|LINK
hello, this didnt worked too.
when there is a null cell in my database, the button field gives an error about string format. When it is not null, it doesnt give any errors.
here is the code. i'm waiting your answers.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = (e.CommandSource as Button).NamingContainer as GridViewRow; Label Label1 = row.FindControl("Label1") as Label; Label1.Text = (Convert.ToDouble(string.IsNullOrEmpty(row.Cells[4].Text)? "0" : row.Cells[4].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[5].Text)? "0" : row.Cells[5].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[6].Text)? "0" : row.Cells[6].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[7].Text)? "0" : row.Cells[7].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[8].Text)? "0" : row.Cells[8].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[9].Text)? "0" : row.Cells[9].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[10].Text)? "0" : row.Cells[10].Text)).ToString(); }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Dec 08, 2012 01:25 AM|LINK
Hello kuthay,
So can you show us your aspx codes?
Kuthay Gumus
Member
13 Points
116 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Dec 08, 2012 11:22 AM|LINK
here is aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication17.WebForm1" %> <!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></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="Black" OnRowCommand="GridView1_RowCommand"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" /> <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="AD_SOYAD" HeaderText="AD_SOYAD" SortExpression="AD_SOYAD" /> <asp:BoundField DataField="MESAI_TIPI" HeaderText="MESAI_TIPI" SortExpression="MESAI_TIPI" /> <asp:BoundField DataField="PAZARTESI" HeaderText="PAZARTESI" SortExpression="PAZARTESI" /> <asp:BoundField DataField="SALI" HeaderText="SALI" SortExpression="SALI" /> <asp:BoundField DataField="CARSAMBA" HeaderText="CARSAMBA" SortExpression="CARSAMBA" /> <asp:BoundField DataField="PERSEMBE" HeaderText="PERSEMBE" SortExpression="PERSEMBE" /> <asp:BoundField DataField="CUMA" HeaderText="CUMA" SortExpression="CUMA" /> <asp:BoundField DataField="CUMARTESI" HeaderText="CUMARTESI" SortExpression="CUMARTESI" /> <asp:BoundField DataField="PAZAR" HeaderText="PAZAR" SortExpression="PAZAR" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="btnCalculate" runat="server" Text="Calculate" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="TOTAL" SortExpression="TOTAL"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TOTAL") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("TOTAL") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="HAM_FM" HeaderText="HAM_FM" SortExpression="HAM_FM" /> <asp:BoundField DataField="GUN" HeaderText="GUN" SortExpression="GUN" /> <asp:BoundField DataField="GK" HeaderText="GK" SortExpression="GK" /> <asp:BoundField DataField="PG" HeaderText="PG" SortExpression="PG" /> <asp:BoundField DataField="GK_PG" HeaderText="GK_PG" SortExpression="GK_PG" /> <asp:BoundField DataField="HAM" HeaderText="HAM" SortExpression="HAM" /> <asp:BoundField DataField="KUM" HeaderText="KUM" SortExpression="KUM" /> <asp:BoundField DataField="GEREKEN" HeaderText="GEREKEN" SortExpression="GEREKEN" /> </Columns> <FooterStyle BackColor="#CCCCCC" /> <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /> <RowStyle BackColor="White" /> <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#808080" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#383838" /> </asp:GridView> </div> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:asgdb01ConnectionString %>" DeleteCommand="DELETE FROM [table] WHERE [ID] = @ID" InsertCommand="INSERT INTO [table] ([AD_SOYAD], [MESAI_TIPI], [PAZARTESI], [SALI], [CARSAMBA], [PERSEMBE], [CUMA], [CUMARTESI], [PAZAR], [TOTAL], [HAM_FM], [GUN], [GK], [PG], [GK_PG], [HAM], [KUM], [GEREKEN]) VALUES (@AD_SOYAD, @MESAI_TIPI, @PAZARTESI, @SALI, @CARSAMBA, @PERSEMBE, @CUMA, @CUMARTESI, @PAZAR, @TOTAL, @HAM_FM, @GUN, @GK, @PG, @GK_PG, @HAM, @KUM, @GEREKEN)" SelectCommand="SELECT * FROM [table]" UpdateCommand="UPDATE [table] SET [AD_SOYAD] = @AD_SOYAD, [MESAI_TIPI] = @MESAI_TIPI, [PAZARTESI] = @PAZARTESI, [SALI] = @SALI, [CARSAMBA] = @CARSAMBA, [PERSEMBE] = @PERSEMBE, [CUMA] = @CUMA, [CUMARTESI] = @CUMARTESI, [PAZAR] = @PAZAR, [TOTAL] = @TOTAL, [HAM_FM] = @HAM_FM, [GUN] = @GUN, [GK] = @GK, [PG] = @PG, [GK_PG] = @GK_PG, [HAM] = @HAM, [KUM] = @KUM, [GEREKEN] = @GEREKEN WHERE [ID] = @ID"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="AD_SOYAD" Type="String" /> <asp:Parameter Name="MESAI_TIPI" Type="String" /> <asp:Parameter Name="PAZARTESI" Type="Int32" /> <asp:Parameter Name="SALI" Type="Int32" /> <asp:Parameter Name="CARSAMBA" Type="Int32" /> <asp:Parameter Name="PERSEMBE" Type="Int32" /> <asp:Parameter Name="CUMA" Type="Int32" /> <asp:Parameter Name="CUMARTESI" Type="Int32" /> <asp:Parameter Name="PAZAR" Type="Int32" /> <asp:Parameter Name="TOTAL" Type="Int32" /> <asp:Parameter Name="HAM_FM" Type="Int32" /> <asp:Parameter Name="GUN" Type="Int32" /> <asp:Parameter Name="GK" Type="Int32" /> <asp:Parameter Name="PG" Type="Int32" /> <asp:Parameter Name="GK_PG" Type="Int32" /> <asp:Parameter Name="HAM" Type="Int32" /> <asp:Parameter Name="KUM" Type="Int32" /> <asp:Parameter Name="GEREKEN" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="AD_SOYAD" Type="String" /> <asp:Parameter Name="MESAI_TIPI" Type="String" /> <asp:Parameter Name="PAZARTESI" Type="Int32" /> <asp:Parameter Name="SALI" Type="Int32" /> <asp:Parameter Name="CARSAMBA" Type="Int32" /> <asp:Parameter Name="PERSEMBE" Type="Int32" /> <asp:Parameter Name="CUMA" Type="Int32" /> <asp:Parameter Name="CUMARTESI" Type="Int32" /> <asp:Parameter Name="PAZAR" Type="Int32" /> <asp:Parameter Name="TOTAL" Type="Int32" /> <asp:Parameter Name="HAM_FM" Type="Int32" /> <asp:Parameter Name="GUN" Type="Int32" /> <asp:Parameter Name="GK" Type="Int32" /> <asp:Parameter Name="PG" Type="Int32" /> <asp:Parameter Name="GK_PG" Type="Int32" /> <asp:Parameter Name="HAM" Type="Int32" /> <asp:Parameter Name="KUM" Type="Int32" /> <asp:Parameter Name="GEREKEN" Type="Int32" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> </form> </body> </html>here is aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Web.Configuration; namespace WebApplication17 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) this.BindGrid(); } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.Add("MON"); dt.Columns.Add("TUE"); dt.Columns.Add("WED"); dt.Columns.Add("THU"); dt.Columns.Add("FRI"); dt.Columns.Add("SAT"); dt.Columns.Add("SUN"); dt.Rows.Add(new Object[] { 14, 15, 16, 17, 18, 19, 20 }); dt.Rows.Add(new Object[] { 24, 25, 26, 27, 28, 29, 30 }); dt.Rows.Add(new Object[] { 34, 35, 36, 37, 38, 39, 40 }); dt.Rows.Add(new Object[] { 44, 45, 46, 47, 48, 49, 50 }); dt.Rows.Add(new Object[] { 54, 55, 56, 57, 58, 59, 60 }); dt.Rows.Add(new Object[] { 64, 65, 66, 67, 68, 69, 70 }); dt.Rows.Add(new Object[] { 74, 75, 76, 77, 78, 79, 80 }); //this.GridView1.DataSource = dt; this.GridView1.DataBind(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = (e.CommandSource as Button).NamingContainer as GridViewRow; Label Label1 = row.FindControl("Label1") as Label; Label1.Text = (Convert.ToDouble(string.IsNullOrEmpty(row.Cells[4].Text)? "0" : row.Cells[4].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[5].Text)? "0" : row.Cells[5].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[6].Text)? "0" : row.Cells[6].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[7].Text)? "0" : row.Cells[7].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[8].Text)? "0" : row.Cells[8].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[9].Text)? "0" : row.Cells[9].Text) + Convert.ToDouble(string.IsNullOrEmpty(row.Cells[10].Text)? "0" : row.Cells[10].Text)).ToString(); } } }that's all.
waiting your answers..
thanks.
Kuthay Gumus
Member
13 Points
116 Posts
Re: Object reference not set to an instance of an object - Inserting Results to Database
Dec 08, 2012 01:58 PM|LINK
hello dacker,
i sent them. Pazartesi- Salı - Carsamba -....... Pazar is days of week in other language. Dont mind. them.
waiting your answers.
thanks.