I need to multiply two columns in my gridview and display the result in a third column. However, I am encountering a null reference exception error in the rowdatabound code. How can I resolve this issue? Below is my .cs code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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.Globalization;
public partial class ProjectBilling : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//AppSettingsReader configReader = new AppSettingsReader();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtHour = (TextBox)e.Row.FindControl("Hour");
TextBox txtrate = (TextBox)e.Row.FindControl("RatePerHour");
TextBox TextBoxInsertTotal = (TextBox)e.Row.FindControl("Total");
txtHour.Attributes["onKeyup"] = "javascript: return multiplication('" + Convert.ToInt32(txtHour.Text) + "','" + Convert.ToDouble(txtrate.Text) + "','" + Convert.ToInt32(TextBoxInsertTotal.ClientID) + "')";
txtrate.Attributes["onKeyup"] = "javascript: return multiplication('" + Convert.ToInt32(txtHour.Text) + "','" + Convert.ToDouble(txtrate.Text) + "','" + Convert.ToInt32(TextBoxInsertTotal.ClientID) + "')";
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected double CalculateTotal(double RatePerHour, int Hour)
{
return RatePerHour * Hour;
}
protected void Add_Click(object sender, EventArgs e)
{
try
{
SqlDataSource1.InsertParameters["Hour"].DefaultValue =
((TextBox)GridView1.FooterRow.FindControl("txtHour")).Text;
SqlDataSource1.InsertParameters["ProjectType"].DefaultValue =
((DropDownList)GridView1.FooterRow.FindControl("ddlName")).SelectedValue;
SqlDataSource1.InsertParameters["ProjectName"].DefaultValue =
((TextBox)GridView1.FooterRow.FindControl("projectnameTextBox")).Text;
SqlDataSource1.InsertParameters["Month"].DefaultValue =
((DropDownList)GridView1.FooterRow.FindControl("ddlmonth")).SelectedValue;
SqlDataSource1.InsertParameters["Year"].DefaultValue =
((DropDownList)GridView1.FooterRow.FindControl("ddlyear")).SelectedValue;
SqlDataSource1.InsertParameters["RatePerHour"].DefaultValue =
((TextBox)GridView1.FooterRow.FindControl("txtrate")).Text;
SqlDataSource1.Insert();
}
catch (Exception ex)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
Here is my aspx code (the total calculation should change after editing the hour or RatePerHour):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProjectBilling.aspx.cs" Inherits="ProjectBilling" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Project Billing</title>
<link href="StyleSheet1.css" rel="stylesheet" />
</head>
<script>
function multiplication(hour, rate, TextBoxInsertTotal) {
var hour = document.getElementById(txtHour).value;
var rate = document.getElementById(txtrate).value;
document.getElementById(TextBoxInsertTotal).value = hour * rate;
}
</script>
<body>
<form id="form1" runat="server">
<div>
<div id="menu">
<ul>
<li><a href="HomePage.aspx">Home</a></li>
<li><a href="ProjectEntry.aspx">Projects</a></li>
<li><a href="ProjectBilling.aspx">Project Billing</a></li>
<li><a href="report1.aspx">Report</a></li>
<li><a href="login.aspx">Logout</a></li>
</ul>
</div>
<br />
<br />
<h2>Project Billing</h2>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" align="center" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ProjectBillingId" ForeColor="#333333" GridLines="None" ShowFooter="True" DataSourceID="SqlDataSource1" AllowPaging="True" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
... (columns omitted for brevity)
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor...
I have tried various JavaScript options within my code, but none of them seem to work. Any sample code you could provide would be greatly appreciated.