Hi,
I am getting this error at run time:
Object reference not set to an instance of an object.
Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 59: SqlCommand sqlCmd = new SqlCommand("SELECT function_name, system_name, interface_direction FROM header WHERE office_cd = @Value1 AND trade_cd = @Value2", connection);
Line 60: SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);Line 61: sqlCmd.Parameters.AddWithValue("@Value1", Office_cd.SelectedItem.Text); Line 62: sqlCmd.Parameters.AddWithValue("@Value2", trade_cd.SelectedItem.Text); |
Can someone please help me on this one. I have attached the relevant code behind which may be the source of error.
Thanks.
using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Data.SqlClient; using System.Globalization; using System.Text.RegularExpressions; using System.Security.Principal; // here is the security namespace you need using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections.Specialized; using System.Text; namespace GISA { public partial class _Default : System.Web.UI.Page { private string GetConnectionString() { return ConfigurationManager.ConnectionStrings["GISAConnectionString"].ConnectionString; } private void ShowNoResultFound(DataTable source, GridView gv) { source.Rows.Add(source.NewRow()); // create a new blank row to the DataTable // Bind the DataTable which contain a blank row to the GridView gv.DataSource = source; gv.DataBind(); // Get the total number of columns in the GridView to know what the Column Span should be int columnsCount = gv.Columns.Count; gv.Rows[0].Cells.Clear();// clear all the cells in the row gv.Rows[0].Cells.Add(new TableCell()); //add a new blank cell gv.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell //You can set the styles here gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center; gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red; gv.Rows[0].Cells[0].Font.Bold = true; //set No Results found to the new added cell gv.Rows[0].Cells[0].Text = "NO RECORD FOUND!"; } private void BindGridView() { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); SqlCommand sqlCmd = new SqlCommand("SELECT function_name, system_name, interface_direction FROM header WHERE office_cd = @Value1 AND trade_cd = @Value2", connection); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlCmd.Parameters.AddWithValue("@Value1", Office_cd.SelectedItem.Text); sqlCmd.Parameters.AddWithValue("@Value2", trade_cd.SelectedItem.Text); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { GridView1.DataSource = dt; GridView1.DataBind(); } else { ShowNoResultFound(dt, GridView1); } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } }