ช่วยหน่อยครับ Error : 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 155: {
Line 156:
Line 157: if (ctrl.GetType() == typeof(TextBox))
Line 158: {
Line 159: ((TextBox)ctrl).Text = value;
Source File: c:\Users\001315\Desktop\SouceCode\ECN_Dev\ECN_Dev\matchControl.cs Line: 157
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ECN_Dev.matchControl.setCtrl(Control ctrl, String value) in c:\Users\001315\Desktop\SouceCode\ECN_Dev\ECN_Dev\matchControl.cs:157
ECN_Dev.matchControl.DTrowtocon(String showon, DataTable iDT, ContentPlaceHolder me) in c:\Users\001315\Desktop\SouceCode\ECN_Dev\ECN_Dev\matchControl.cs:185
ECN_Dev.ECN_Review.GetECN(String ECN_NO) in c:\Users\001315\Desktop\SouceCode\ECN_Dev\ECN_Dev\ECN_Review.aspx.cs:94
ECN_Dev.ECN_Review.Page_Load(Object sender, EventArgs e) in c:\Users\001315\Desktop\SouceCode\ECN_Dev\ECN_Dev\ECN_Review.aspx.cs:207
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772Tag : .NET, Web (ASP.NET), VB.NET, C#, VS 2012 (.NET 4.x)
ประวัติการแก้ไข 2013-08-13 08:50:28
Date :
2013-08-13 08:49:53
By :
kongkkng
View :
8278
Reply :
2
Code (C#)
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.Text;
using System.IO;
using System.Data.Sql;
using System.Configuration;
using System.Net;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace ECN_Dev
{
public class matchControl
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
string sqlstr;
datacls dc = new datacls();
protected string MatchSQL(string showon)
{
string str;
str = "select * from match_control as t1 inner join show_control as t2 on t1.m_fields=t2.m_fields where c_showon like '" + showon + "' order by c_order";
return str;
}
public void toControl(string showon, GridView GV)
{
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count > 0)
{
foreach (DataRow dr in dc.dt.Rows)
{
BoundField bf = new BoundField();
bf.DataField = dr["m_fields"].ToString();
bf.HeaderText = dr["m_des"].ToString();
if (dr["m_ftype"].ToString() == "date") { bf.DataFormatString = "{0:MM/dd/yyyy}"; }
GV.Columns.Add(bf);
}
}
}
public DataTable CreateDT(string showon)
{
DataTable dt = new DataTable(showon);
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count > 0)
{
foreach (DataRow dr in dc.dt.Rows)
{
DataColumn dtc = new DataColumn((string)dr["m_fields"]);
dt.Columns.Add(dtc);
}
}
return dt;
}
public string SelectbyOrder(string showon)
{
string str = "";
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count > 0)
{
foreach (DataRow dr in dc.dt.Rows)
{
str += "," + dr["m_fields"].ToString();
}
}
str = str.Substring(1);
return str;
}
public DataRow addRow(string showon, DataTable dt, ContentPlaceHolder me, string key)
{
DataRow dr = dt.NewRow();
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count > 0)
{
foreach (DataRow dr1 in dc.dt.Rows)
{
if (!key.Contains(dr1["m_fields"].ToString()))
{
string str = getString(me.FindControl((string)dr1["m_con"]));
if (dr1["m_ftype"].ToString() == "date" && str == "")
dr[dr1["m_fields"].ToString()] = DBNull.Value;
else
dr[dr1["m_fields"].ToString()] = str;
}
}
}
return dr;
}
protected string getString(Control ctrl)
{
string result;
if (ctrl.GetType() == typeof(TextBox)) result = ((TextBox)ctrl).Text;
else if (ctrl.GetType() == typeof(DropDownList)) result = ((DropDownList)ctrl).SelectedValue;
else if (ctrl.GetType() == typeof(RadioButtonList)) result = ((RadioButtonList)ctrl).SelectedValue;
else if (ctrl.GetType() == typeof(Label)) result = ((Label)ctrl).Text;
else result = "";
return result;
}
public void GVrowtocon(string showon, GridViewRow row, ContentPlaceHolder me)
{
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count > 0)
{
for (int i = 0; i < dc.dt.Rows.Count; i++)
{
setCtrl(me.FindControl((string)dc.dt.Rows[i]["m_con"]), Modcls.Nullnbsp(row.Cells[i + 1].Text));
}
}
}
protected void setCtrl(Control ctrl, string value)
{
if (ctrl.GetType() == typeof(TextBox))
{
((TextBox)ctrl).Text = value;
}
else if (ctrl.GetType() == typeof(DropDownList))
{
((DropDownList)ctrl).SelectedValue = value;
}
else if (ctrl.GetType() == typeof(RadioButtonList))
{
((RadioButtonList)ctrl).SelectedValue = value;
}
}
public void DTrowtocon(string showon, DataTable iDT, ContentPlaceHolder me)
{
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count == 0) return;
foreach (DataRow dr in dc.dt.Rows)
{
if (dr["m_ftype"].ToString() == "date")
setCtrl(me.FindControl((string)dr["m_con"]), Modcls.ConvertDate(iDT.Rows[0][dr["m_fields"].ToString()].ToString()));
else
setCtrl(me.FindControl((string)dr["m_con"]), iDT.Rows[0][dr["m_fields"].ToString()].ToString());
}
}
public void updateSQL(string Table, string showon, ContentPlaceHolder me, string Key, string ExStr = "")
{
string wheresql = "", rowsql = "";
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count > 0)
sqlstr = " begin ";
sqlstr += "update " + Table + " set ";
foreach (DataRow dr in dc.dt.Rows)
{
if (!Key.Contains(dr["m_fields"].ToString()))
{
if (dr["m_ftype"].ToString() == "date")
rowsql += "," + dr["m_fields"] + "=" + Modcls.NullDate(getString(me.FindControl((string)dr["m_con"]))) + "";
else
rowsql += "," + dr["m_fields"] + "='" + getString(me.FindControl((string)dr["m_con"])) + "'";
}
else
{
//string[] KeyArr = Key.Split(',');
// foreach (string iKey in KeyArr)
//{
wheresql += "and " + dr["m_fields"] + " like '" + getString(me.FindControl((string)dr["m_con"])) + "'";
//}
}
}
wheresql = wheresql.Substring(3);
wheresql = " where " + wheresql;
rowsql = rowsql.Substring(1);
sqlstr += rowsql + ExStr + wheresql;
sqlstr += " end ";
dc.RunCommand(conn, sqlstr);
}
public void inserSQLGV(string Table, string showon, ContentPlaceHolder me, GridView gv1, string key)
{
dc.QryDT(conn, MatchSQL(showon));
if (dc.dt.Rows.Count == 0) return;
if (gv1.Rows.Count == 0) return;
for (int i = 0; i < gv1.Rows.Count; i++)
{
sqlstr = " begin ";
sqlstr += "insert into " + Table + "(";
string fsql = "", vsql = "";
foreach (DataRow dr in dc.dt.Rows)
{
if (!key.Contains(dr["m_fields"].ToString()))
{
fsql += ",[" + dr["m_fields"] + "]";
if (dr["c_ongv"].ToString() != "")
{
if (dr["m_ftype"].ToString() == "date")
vsql += "," + Modcls.NullDate(gv1.Rows[i].Cells[(int)dr["c_order"]].Text) + "";
else
vsql += ",'" + Modcls.Nullnbsp(gv1.Rows[i].Cells[(int)dr["c_order"]].Text) + "'";
}
else
{
if (dr["m_ftype"].ToString() == "date")
vsql += "," + Modcls.NullDate(getString(me.FindControl((string)dr["m_con"]))) + "";
else
vsql += ",'" + Modcls.Nullnbsp(getString(me.FindControl((string)dr["m_con"]))) + "'";
}
}
}
vsql = vsql.Substring(1);
fsql = fsql.Substring(1);
sqlstr += fsql + ") values(" + vsql + ") end";
dc.RunCommand(conn, sqlstr);
}
}
}
}
Date :
2013-08-13 08:52:16
By :
kongkkng
แก้ได้และครับ
protected void setCtrl(Control ctrl, string value)
{
if (ctrl != null && ctrl.GetType() == typeof(TextBox))
{
((TextBox)ctrl).Text = value;
}
else if (ctrl != null && ctrl.GetType() == typeof(DropDownList))
{
((DropDownList)ctrl).SelectedValue = value;
}
else if (ctrl != null && ctrl.GetType() == typeof(RadioButtonList))
{
((RadioButtonList)ctrl).SelectedValue = value;
}
}
เพิ่ม ctrl != null &&
Date :
2013-08-16 12:54:42
By :
kongkkng
Load balance : Server 04