I am trying to Serialize/Deserialize a object, but I am getting an "ArgumentException" on deserialization.
My Object is this:
public class Findoc
{
public Findoc()
{
}
private string _ID = string.Empty;
public string ID
{
get { return this._ID; }
set { _ID = value; }
}
private int _lastindex;
public int lastindex
{
get { return this._lastindex; }
set { _lastindex = value; }
}
private string _SilogiDate;
public string SilogiDate
{
get { return this._SilogiDate; }
set { _SilogiDate = value; }
}
private Truck _TR;
public Truck TR
{
get { return this._TR; }
set { _TR = value; }
}
private Routing _RT;
public Routing RT
{
get { return this._RT; }
set { _RT = value; }
}
private KentroKostous _KK;
public KentroKostous KK
{
get { return this._KK; }
set { _KK = value; }
}
private Busunit _BU;
public Busunit BU
{
get { return this._BU; }
set { _BU = value; }
}
private string _FINCODE = string.Empty;
public string FINCODE
{
get { return this._FINCODE; }
set { _FINCODE = value; }
}
private string _FINSTATE = "";
public string FINSTATE
{
get { return this._FINSTATE; }
set { _FINSTATE = value; }
}
private string _STAGE = "";
public string STAGE
{
get { return this._STAGE; }
set { _STAGE = value; }
}
private string _SPCS = "";
public string SPCS
{
get { return this._SPCS; }
set { _SPCS = value; }
}
private string _SPCSCODE = "";
public string SPCSCODE
{
get { return this._SPCSCODE; }
set { _SPCSCODE = value; }
}
private string _MTRSTS = "";
public string MTRSTS
{
get { return this._MTRSTS; }
set { _MTRSTS = value; }
}
private string _PARAGOMENO = "";
public string PARAGOMENO
{
get { return this._PARAGOMENO; }
set { _PARAGOMENO = value; }
}
private double _PARAGOMENOQTY1;
public double PARAGOMENOQTY1
{
get { return this._PARAGOMENOQTY1; }
set { _PARAGOMENOQTY1 = value; }
}
private double _PARAGOMENOQTY2;
public double PARAGOMENOQTY2
{
get { return this._PARAGOMENOQTY2; }
set { _PARAGOMENOQTY2 = value; }
}
private Boolean _PARAGOMENOUSESN;
public Boolean PARAGOMENOUSESN
{
get { return this._PARAGOMENOUSESN; }
set { _PARAGOMENOUSESN = value; }
}
private Boolean _EDITABLE = true;
public Boolean EDITABLE
{
get { return this._EDITABLE; }
set { _EDITABLE = value; }
}
private Boolean _ISPRINT;
public Boolean ISPRINT
{
get { return this._ISPRINT; }
set { _ISPRINT = value; }
}
private Boolean _ISCANCELED;
public Boolean ISCANCELED
{
get { return this._ISCANCELED; }
set { _ISCANCELED = value; }
}
private int _SOSOURCE;
public int SOSOURCE
{
get { return this._SOSOURCE; }
set { _SOSOURCE = value; }
}
private Series _series;
public Series Series
{
get { return this._series; }
set { _series = value; }
}
private string _TICK = string.Empty;
public string TICK
{
get { return this._TICK; }
set { _TICK = value; }
}
private string _COMMENTS = "";
public string COMMEMTS
{
get { return this._COMMENTS; }
set { _COMMENTS = value; }
}
private string _COMMENTS1 = "";
public string COMMEMTS1
{
get { return this._COMMENTS1; }
set { _COMMENTS1 = value; }
}
private string _COMMENTS2 = "";
public string COMMEMTS2
{
get { return this._COMMENTS2; }
set { _COMMENTS2 = value; }
}
private string _karfoto_fincode = "";//gia tis eisprakseis
public string Karfoto_fincode
{
get { return this._karfoto_fincode; }
set { _karfoto_fincode = value; }
}
private List<Mtrline> _Mtrlines;
public List<Mtrline> Mtrlines
{
get { return this._Mtrlines; }
set { _Mtrlines = value; }
}
}
public class SnLine
{
private string _TICK;
public string TICK
{
get { return this._TICK; }
set { _TICK = value; }
}
private string _sn;
public string sn
{
get { return this._sn; }
set { _sn = value; }
}
}
public class Mtrline
{
public Mtrline(Findoc findoc)
{
}
private List<SnLine> _SnLines;
public List<SnLine> SnLines
{
get { return this._SnLines; }
set { _SnLines = value; }
}
private int _position;
public int position
{
get { return this._position; }
set { _position = value; }
}
private string _guid;
public string guid
{
get { return this._guid; }
set { _guid = value; }
}
private WhouseObj _Whouse1;
public WhouseObj Whouse1
{
get { return this._Whouse1; }
set { _Whouse1 = value; }
}
private WhouseObj _Whouse2;
public WhouseObj Whouse2
{
get { return this._Whouse2; }
set { _Whouse2 = value; }
}
private string _WHOUSE = "";
public string WHOUSE
{
get { return this._WHOUSE; }
set { _WHOUSE = value; }
}
private string _WHOUSESEC = "";
public string WHOUSESEC
{
get { return this._WHOUSESEC; }
set { _WHOUSESEC = value; }
}
private string _SPCS;
public string SPCS
{
get { return this._SPCS; }
set { _SPCS = value; }
}
private string _MPKCODE;
public string MPKCODE
{
get { return this._MPKCODE; }
set { _MPKCODE = value; }
}
private string _whousebin1remain;
public string Whousebin1remain
{
get { return this._whousebin1remain; }
set { _whousebin1remain = value; }
}
private string _whousebin2remain;
public string Whousebin2remain
{
get { return this._whousebin2remain; }
set { _whousebin2remain = value; }
}
private int _NEWLINE;
public int NEWLINE
{
get { return this._NEWLINE; }
set { _NEWLINE = value; }
}
private string _CCCPACK1;
public string CCCPACK1
{
get {
if (_CCCPACK1 == null)
return "";
else
return this._CCCPACK1;
}
set { _CCCPACK1 = value; }
}
public string _CCCPACK2;
public string CCCPACK2
{
get
{
if (_CCCPACK2 == null)
return "";
else
return this._CCCPACK2;
}
set { _CCCPACK2 = value; }
}
private string _PAKETO = "";
public string PAKETO
{
get { return this._PAKETO; }
set { _PAKETO = value; }
}
private string _TICK;
public string TICK
{
get { return this._TICK; }
set { _TICK = value; }
}
private string _AAA;
public string AAA
{
get { return this._AAA; }
set { _AAA = value; }
}
private MtrlModel _MTRL_Object;
public MtrlModel MTRL_Object
{
get { return this._MTRL_Object; }
set { _MTRL_Object = value; }
}
private double _SUMQTY; // SUMQTY ana eidos
public double SUMQTY
{
get { return this._SUMQTY; }
set { _SUMQTY = value; }
}
private double _SUMQTY2; // SUMQTY ana eidos
public double SUMQTY2
{
get { return this._SUMQTY2; }
set { _SUMQTY2 = value; }
}
private double _QTY; // posotita T
public double QTY
{
get { return this._QTY; }
set { _QTY = value; }
}
private double _QTY1;
public double QTY1
{
get { return this._QTY1; }
set { _QTY1 = value; }
}
private double _QTY2;
public double QTY2
{
get { return this._QTY2; }
set { _QTY2 = value; }
}
private double _QTYP;
public double QTYP
{
get { return this._QTYP; }
set { _QTYP = value; }
}
private string _FIELDS = "";
public string FIELDS
{
get { return this._FIELDS; }
set { _FIELDS = value; }
}
private string _COMMENTS = "";
public string COMMENTS
{
get { return this._COMMENTS; }
set { _COMMENTS = value; }
}
private string _COMMENTS1 = "";
public string COMMENTS1
{
get { return this._COMMENTS1; }
set { _COMMENTS1 = value; }
}
private string _COMMENTS2 = "";
public string COMMENTS2
{
get { return this._COMMENTS2; }
set { _COMMENTS2 = value; }
}
private CDIMLINE _CDIMNO1_Object;
public CDIMLINE CDIMNO1_Object
{
get { return this._CDIMNO1_Object; }
set { _CDIMNO1_Object = value; }
}
private string _CDIMNO1;
public string CDIMNO1
{
get
{
if (this._CDIMNO1_Object != null)
return this._CDIMNO1_Object.NAME;
else
return "";
}
set { _CDIMNO1 = value; }
}
private CDIMLINE _CDIMNO2_Object;
public CDIMLINE CDIMNO2_Object
{
get { return this._CDIMNO2_Object; }
set { _CDIMNO2_Object = value; }
}
private string _CDIMNO2;
public string CDIMNO2
{
get
{
if (this._CDIMNO2_Object != null)
return this._CDIMNO2_Object.NAME;
else
return "";
}
set { _CDIMNO2 = value; }
}
private CDIMLINE _CDIMNO3_Object;
public CDIMLINE CDIMNO3_Object
{
get { return this._CDIMNO3_Object; }
set { _CDIMNO3_Object = value; }
}
private string _CDIMNO3;
public string CDIMNO3
{
get
{
if (this._CDIMNO3_Object != null)
return this._CDIMNO3_Object.NAME;
else
return "";
}
set { _CDIMNO3 = value; }
}
private Lot _LOT;
public Lot LOT
{
get { return this._LOT; }
set { _LOT = value; }
}
private string _CODE = "";
public string CODE
{
get { return this._MTRL_Object.CODE; }
set { _CODE = value; }
}
private string _MTRPLACE = "";
public string MTRPLACE
{
get { return this._MTRL_Object.MTRPLACE; }
set { _MTRPLACE = value; }
}
private double _SUMWHOUSE;
public double SUMWHOUSE
{
get { return this._MTRL_Object.REMAIN; }
set { _SUMWHOUSE = value; }
}
private double _WHOUSE_SERIES;
public double WHOUSE_SERIES
{
get { return this._MTRL_Object.WHOUSE_SERIES_REMAIN; }
set { _WHOUSE_SERIES = value; }
}
private string _CODE1 = "";
public string CODE1
{
get { return this._MTRL_Object.CODE1; }
set { _CODE1 = value; }
}
private string _CODE2 = "";
public string CODE2
{
get { return this._MTRL_Object.CODE2; }
set { _CODE2 = value; }
}
private string _NAME = "";
public string NAME
{
get { return this._MTRL_Object.NAME; }
set { _NAME = value; }
}
private string _MTRL = "";
public string MTRL
{
get { return this._MTRL_Object.MTRL; }
set { _MTRL = value; }
}
private string _MTRUNIT1 = "";
public string MTRUNIT1
{
get
{
if (this._MTRL_Object.MTRUNIT1 != null)
return this._MTRL_Object.MTRUNIT1.name;
else
return "";
}
set { _MTRUNIT1 = value; }
}
private string _MTRUNIT2 = "";
public string MTRUNIT2
{
get
{
if (this._MTRL_Object.MTRUNIT2 != null)
return this._MTRL_Object.MTRUNIT2.name;
else
return "";
}
set { _MTRUNIT2 = value; }
}
private string _MTRUNIT3 = "";
public string MTRUNIT3
{
get
{
if (this._MTRL_Object.MTRUNIT3 != null)
return this._MTRL_Object.MTRUNIT3.name;
else
return "";
}
set { _MTRUNIT3 = value; }
}
private string _MTRUNIT4 = "";
public string MTRUNIT4
{
get
{
if (this._MTRL_Object.MTRUNIT4 != null)
return this._MTRL_Object.MTRUNIT4.name;
else
return "";
}
set { _MTRUNIT4 = value; }
}
private string _CRLOTCODE = "";
public string CRLOTCODE
{
get { return this._CRLOTCODE; }
set { _CRLOTCODE = value; }
}
private string _CRLOTCODE1 = "";
public string CRLOTCODE1
{
get { return this._CRLOTCODE1; }
set { _CRLOTCODE1 = value; }
}
private string _CRLOTCODE2 = "";
public string CRLOTCODE2
{
get { return this._CRLOTCODE2; }
set { _CRLOTCODE2 = value; }
}
private string _CRLOTFDATE = "";
public string CRLOTFDATE
{
get { return this._CRLOTFDATE; }
set { _CRLOTFDATE = value; }
}
private string _SCANEDCODE = "";
public string SCANEDCODE
{
get { return this._SCANEDCODE; }
set { _SCANEDCODE = value; }
}
private Thesi _WHOUSEBIN1_Object;
public Thesi WHOUSEBIN1_Object
{
get { return this._WHOUSEBIN1_Object; }
set { _WHOUSEBIN1_Object = value; }
}
private string _WHOUSEBIN1;
public string WHOUSEBIN1
{
get
{
if (this._WHOUSEBIN1_Object != null)
return this._WHOUSEBIN1_Object.name;
else
return "";
}
set { _WHOUSEBIN1 = value; }
}
private Thesi _WHOUSEBIN2_Object;
public Thesi WHOUSEBIN2_Object
{
get { return this._WHOUSEBIN2_Object; }
set { _WHOUSEBIN2_Object = value; }
}
private string _WHOUSEBIN2;
public string WHOUSEBIN2
{
get
{
if (this._WHOUSEBIN2_Object != null)
return this._WHOUSEBIN2_Object.name;
else
return "";
}
set { _WHOUSEBIN2 = value; }
}
private double _ANAMENOMENA;
public double ANAMENOMENA
{
get { return this._ANAMENOMENA; }
set { _ANAMENOMENA = value; }
}
private string _FINCODE = "";
public string FINCODE
{
get { return this._FINCODE; }
set { _FINCODE = value; }
}
private string _FINDOC = "";
public string FINDOC
{
get { return this._FINDOC; }
set { _FINDOC = value; }
}
private string _SODTYPE = "";
public string SODTYPE
{
get { return this._SODTYPE; }
set { _SODTYPE = value; }
}
private string _STATUS = "";
public string STATUS
{
get { return this._STATUS; }
set { _STATUS = value; }
}
private string _AA = "";
public string AA
{
get { return this._AA; }
set { _AA = value; }
}
private string _GUARANTY_SNCODE = "";
public string GUARANTY_SNCODE
{
get { return this._GUARANTY_SNCODE; }
set { _GUARANTY_SNCODE = value; }
}
}
I serialize it with the following code:
string data = Newtonsoft.Json.JsonConvert.SerializeObject(UniversalModel.Parastatiko,Formatting.None , new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
When I try to deserialize back to the object I get the exception error: ArgumentException.
The code to deserialize is this.
Findoc FFF = Newtonsoft.Json.JsonConvert.DeserializeObject<Findoc>(data, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Any tips on what I am doing wrong?
It may just be a typo in the question but separating the Serializer arguments on to their own lines shows you are passing a UniversalModel.Parastatiko in as the first argument...which I don't think is correct.
string data = Newtonsoft.Json.JsonConvert.SerializeObject(
UniversalModel.Parastatiko,
Formatting.None,
new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore
}
);
public class AMCOMDB : DataContext
{
public Table<student> allStudents;
public Table<aClass> allClasses;
public AMCOMDB(string connection) : base(connection) { }
}
[Table(Name = "student")]
public class student
{
private string _studentName;
[Column(IsPrimaryKey =true,Storage ="_studentName")]
public string studentName
{
get { return this._studentName; }
set { this._studentName = value; }
}
private string _LARType;
[Column(Storage ="_LARType")]
public string LARType
{
get { return this._LARType; }
set { this._LARType = value; }
}
private string _studentType;
[Column(Storage = "_studentType")]
public string studentType
{
get { return this._studentType; }
set { this._studentType = value; }
}
private string _aviationLevel;
[Column(Storage = "_aviationLevel")]
public string aviationLevel
{
get { return this._aviationLevel; }
set { this._aviationLevel = value; }
}
private string _airDefenseLevel;
[Column(Storage = "_airDefenseLevel")]
public string airDefenseLevel
{
get
{
return this._airDefenseLevel;
}
set
{
this._airDefenseLevel = value;
}
}
private string _emergencyContact;
[Column(Storage = "_emergencyContact")]
public string emergencyContact
{
get
{
return this._emergencyContact;
}
set
{
this._emergencyContact = value;
}
}
[Table(Name = "grades")]
public class grades
{
private string _studentName;
[Column(IsPrimaryKey = true, Storage = "_studentName")]
public string studentName
{
get
{
return this._studentName;
}
set
{
this._studentName = value;
}
}
private int _ET;
[Column(Storage = "_ET")]
public int ET
{
get
{
return this._ET;
}
set
{
this._ET = value;
}
}
private int _CP;
[Column(Storage = "_CP")]
public int CP
{
get
{
return this._CP;
}
set
{
this._CP = value;
}
}
private int _SB;
[Column(Storage = "_SB")]
public int SB
{
get
{
return this._SB;
}
set
{
this._SB = value;
}
}
private int _EC;
[Column(Storage = "_EC")]
public int EC
{
get
{
return this._EC;
}
set
{
this._EC = value;
}
}
private int _finalGrade;
[Column(Storage = "_finalGrade")]
public int finalGrade
{
get
{
return this._finalGrade;
}
set
{
this._finalGrade = value;
}
}
}
[Table(Name = "classes")]
public class aClass
{
private string _classNumber;
[Column(IsPrimaryKey = true, Storage = "_classNumber")]
public string classNumber
{
get
{
return this._classNumber;
}
set
{
this._classNumber = value;
}
}
private string _courseSeries;
[Column(Storage = "_courseSeries")]
public string courseSeries
{
get
{
return this._courseSeries;
}
set
{
this._courseSeries = value;
}
}
private string _courseNumber;
[Column(Storage = "_courseNumber")]
public string courseNumber
{
get
{
return this._courseNumber;
}
set
{
this._courseNumber = value;
}
}
private string _distanceLearning;
[Column(Storage = "_distanceLearning")]
public string distanceLearning
{
get
{
return this._distanceLearning;
}
set
{
this._distanceLearning = value;
}
}
private string _classStartDate;
[Column(Storage = "_classStartDate")]
public string classStartDate
{
get
{
return this._classStartDate;
}
set
{
this._classStartDate = value;
}
}
private string _classEndDate;
[Column(Storage = "_classEndDate")]
public string classEndDate
{
get
{
return this._classEndDate;
}
set
{
this._classEndDate = value;
}
}
private string _primaryInstructor;
[Column(Storage = "_primaryInstructor")]
public string primaryInstructor
{
get
{
return this._primaryInstructor;
}
set
{
this._primaryInstructor = value;
}
}
private string _secondaryInstructor;
[Column(Storage = "_secondaryInstructor")]
public string secondaryInstructor
{
get
{
return this._secondaryInstructor;
}
set
{
this._secondaryInstructor = value;
}
}
private string _location;
[Column(Storage = "_location")]
public string location
{
get
{
return this._location;
}
set
{
this._location = value;
}
}
private string _TDYCosts;
[Column(Storage = "_TDYCosts")]
public string TDYCosts
{
get
{
return this._TDYCosts;
}
set
{
this._TDYCosts = value;
}
}
private string _studentCount;
[Column(Storage = "_studentCount")]
public string studentCount
{
get
{
return this._studentCount;
}
set
{
this._studentCount = value;
}
}
private List<grades> _classGrades;
[Column(Storage = "_classGrades")]
public List<grades> classGrades
{
get
{
return this._classGrades;
}
set
{
this._classGrades = value;
}
}
AMCOMDB ADB = new AMCOMDB(connectionString);
if (ADB.DatabaseExists())
{
var stud = ADB.GetTable<student>();
var clas = ADB.GetTable<aClass>();
IQueryable<string> query = from c in stud
where c.studentName.Length > 5
orderby c.studentName.Length
select c.studentName.ToUpper();
foreach (string name in query)
{
}
//var q = from a in ADB.GetTable<student>()
// select a;
//dtStudents = LinqQuerytoDataTable(q);
//var q1 = from a in ADB.GetTable<aClass>()
// select a;
//aClass c = new aClass();
//dtClasses = reformatDataTable(q1);
}
I receive the following when I try to get information from the database at (foreach (string name in query))
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Invalid object name 'student'.
I also get this when I first create the database:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.Linq.dll
Additional information: Unable to determine SQL type for 'System.Collections.Generic.List`1[WindowsFormsApplication1.Form1+grades]'.
remove the word Name that is what worked for me
[Table("student")]
public class student
i have collection of class "Microsoft.Exchange.Management.TransportLogSearchTasks.MessageTrackingEvent"
this is microsoft class.
this class build like the following class
public class MTE
{
private DateTime timestamp;
private string clientIp;
private string clientHostname;
private string serverIp;
private string serverHostname;
private string sourceContext;
private string connectorId;
private string source;
private string eventId;
private string internalMessageId;
private string messageId;
private string[] recipients;
private string[] recipientStatus;
private int? totalBytes;
private int recipientCount;
private string relatedRecipientAddress;
private string[] reference;
private string messageSubject;
private string sender;
private string returnPath;
private string messageInfo;
private new DateTime Timestamp
{
get { return timestamp; }
set { timestamp = value; }
}
public new string ClientIp
{
get { return clientIp; }
set { clientIp = value; }
}
public new string ClientHostname
{
get { return clientHostname; }
set { clientHostname = value; }
}
public new string ServerIp
{
get { return serverIp; }
set { serverIp = value; }
}
public new string ServerHostname
{
get { return serverHostname; }
set { serverHostname = value; }
}
public new string SourceContext
{
get { return sourceContext; }
set { sourceContext = value; }
}
public new string ConnectorId
{
get { return connectorId; }
set { connectorId = value; }
}
public new string Source
{
get { return source; }
set { source = value; }
}
public new string EventId
{
get { return eventId; }
set { eventId = value; }
}
public new string InternalMessageId
{
get { return internalMessageId; }
set { internalMessageId = value; }
}
public new string MessageId
{
get { return messageId; }
set { messageId = value; }
}
public new string[] Recipients
{
get { return recipients; }
set { recipients = value; }
}
public new string RecipientStatus
{
get { return[] recipientStatus; }
set { recipientStatus = value; }
}
public new int? TotalBytes
{
get { return totalBytes; }
set { totalBytes = value; }
}
public new int RecipientCount
{
get { return recipientCount; }
set { recipientCount = value; }
}
public new string RelatedRecipientAddress
{
get { return relatedRecipientAddress; }
set { relatedRecipientAddress = value; }
}
public new string Reference
{
get { return[] reference; }
set { reference = value; }
}
public new string MessageSubject
{
get { return messageSubject; }
set { messageSubject = value; }
}
public new string Sender
{
get { return sender; }
set { sender = value; }
}
public new string ReturnPath
{
get { return returnPath; }
set { returnPath = value; }
}
public new string MessageInfo
{
get { return messageInfo; }
set { messageInfo = value; }
}
}
When i am doing
Collection<MTE> newMTE = new Collection<MTE>();
datagridview1.datasource= newMTE;
datagrid1.datasource = newMTE
on DGV i can see at all the string[] properties
on the datagrid i can see them but as property type and not as a value
image example:
http://farm6.staticflickr.com/5519/10725217114_226ddd96c7_m.jpg
my collection have millions of rows, so i need an easy to process solution.
thank you all for the help
newMTE is a collection itself and your string[] are collection of items too (string array).
You can use something like Grouping or Show Row Details to show subrows of a row.
Examples here:
http://www.wpftutorial.net/DataGrid.html
I have this Business Layer/ Contactentry.ascx page which calls a stored prcoedure to insert data into sql database and then the page below calls this business layer method. the issue is this method
aspdotnet.BusinessLogicLayer.ContactEntry AddEntry =
new ContactEntry(Convert.ToInt32(Session["ContactID"].ToString())
,Title,FirstName,MiddleName,LastName,JobTitle,Company,Website,OfficePhone
,HomePhone,Mobile,Fax,OEmail,PEmail,OAStreet,OACity,OAState,OACountry
,OAZipCode,PAStreet,PACity,PAState,PACountry,PAZipCode);
throws an error saying object refernece not set to an instance of an object although I have entered all values for the text fields.
using System;
using System.Data;
using System.Configuration;
using aspdotnet.DataAccessLayer;
namespace aspdotnet.BusinessLogicLayer
{
public class ContactEntry
{
private int _ContactID;
private string _Title;
private string _FirstName;
private string _MiddleName;
private string _LastName;
private string _JobTitle;
private string _Company;
private string _Website;
private string _OfficePhone;
private string _HomePhone;
private string _Mobile;
private string _Fax;
private string _OEmail;
private string _PEmail;
private string _OAStreet;
private string _OACity;
private string _OAState;
private string _OACountry;
private string _OAZipCode;
private string _PAStreet;
private string _PACity;
private string _PAState;
private string _PACountry;
private string _PAZipCode;
public int ContactID
{
get { return _ContactID; }
set { _ContactID = value; }
}
public string Title
{
get { return _Title; }
set { _Title = value; }
}
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}
public string MiddleName
{
get { return _MiddleName; }
set { _MiddleName = value; }
}
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
public string JobTitle
{
get { return _JobTitle; }
set { _JobTitle = value; }
}
public string Company
{
get { return _Company; }
set { _Company = value; }
}
public string Website
{
get { return _Website; }
set { _Website = value; }
}
public string OfficePhone
{
get { return _OfficePhone; }
set { _OfficePhone = value; }
}
public string HomePhone
{
get { return _HomePhone; }
set { _HomePhone = value; }
}
public string Mobile
{
get { return _Mobile; }
set { _Mobile = value; }
}
public string Fax
{
get { return _Fax; }
set { _Fax = value; }
}
public string OEmail
{
get { return _OEmail; }
set { _OEmail = value; }
}
public string PEmail
{
get { return _PEmail; }
set { _PEmail = value; }
}
public string OAStreet
{
get { return _OAStreet; }
set { _OAStreet = value; }
}
public string OACity
{
get { return _OACity; }
set { _OACity = value; }
}
public string OAState
{
get { return _OAState; }
set { _OAState = value; }
}
public string OACountry
{
get { return _OACountry; }
set { _OACountry = value; }
}
public string OAZipCode
{
get { return _OAZipCode; }
set { _OAZipCode = value; }
}
public string PAStreet
{
get { return _PAStreet; }
set { _PAStreet = value; }
}
public string PACity
{
get { return _PACity; }
set { _PACity = value; }
}
public string PAState
{
get { return _PAState; }
set { _PAState = value; }
}
public string PACountry
{
get { return _PACountry; }
set { _PACountry = value; }
}
public string PAZipCode
{
get { return _PAZipCode; }
set { _PAZipCode = value; }
}
public ContactEntry()
{
}
public ContactEntry(int ContactID, string Title, string FirstName, string MiddleName, string LastName, string JobTitle, string Company, string Website, string OfficePhone, string HomePhone, string Mobile, string Fax, string OEmail, string PEmail, string OAStreet, string OACity, string OAState, string OACountry, string OAZipCode, string PAStreet, string PACity, string PAState, string PACountry, string PAZipCode)
{
_ContactID=ContactID;
_Title=Title;
_FirstName = FirstName;
_MiddleName = MiddleName;
_LastName = LastName;
_JobTitle = JobTitle;
_Company = Company;
_Website = Website;
_OfficePhone = OfficePhone;
_HomePhone = HomePhone;
_Mobile = Mobile;
_Fax = Fax;
_OEmail=OEmail;
_PEmail=PEmail;
_OAStreet = OAStreet;
_OACity = OACity;
_OAState = OAState;
_OACountry =OACountry;
_OAZipCode = OAZipCode;
_PAStreet = PAStreet;
_PACity = PACity;
_PAState = PAState;
_PACountry = PACountry;
_PAZipCode = PAZipCode;
}
public bool Save()
{
if (_ContactID == 0)
return Insert();
else
return Update();
}
private bool Insert()
{
_ContactID = Convert.ToInt32(DBTask.ExecuteScalar(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_Insert", _Title, _FirstName, _MiddleName, _LastName, _JobTitle, _Company, _Website, _OfficePhone, _HomePhone, _Mobile, _Fax, _OEmail, _PEmail, _OAStreet, _OACity, _OAState, _OACountry, _OAZipCode, _PAStreet, _PACity, _PAState, _PACountry, _PAZipCode));
return (0 < _ContactID);
}
public static void Remove(int ContactID)
{
DBTask.ExecuteNonQuery(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_Delete", ContactID);
}
private bool Update()
{
try
{
DBTask.ExecuteNonQuery(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_Update", _ContactID, _Title, _FirstName, _MiddleName, _LastName, _JobTitle, _Company, _Website, _OfficePhone, _HomePhone, _Mobile, _Fax, _OEmail, _PEmail, _OAStreet, _OACity, _OAState, _OACountry, _OAZipCode, _PAStreet, _PACity, _PAState, _PACountry, _PAZipCode);
return true;
}
catch
{
return false;
}
}
public void LoadContact(int ContactID)
{
DataSet ds = DBTask.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings[Web.Global.CfgKeyConnString], "ContactInfo_GetContact", ContactID);
DataRow row = ds.Tables[0].Rows[0];
_ContactID=Convert.ToInt32(row["ContactID"].ToString());
_Title=row["Title"].ToString();
_FirstName = row["FirstName"].ToString();
_MiddleName = row["MiddleName"].ToString();
_LastName = row["LastName"].ToString();
_JobTitle = row["JobTitle"].ToString();
_Company = row["Company"].ToString();
_Website = row["Website"].ToString();
_OfficePhone = row["OfficePhone"].ToString();
_HomePhone = row["HomePhone"].ToString();
_Mobile = row["Mobile"].ToString();
_Fax = row["Fax"].ToString();
_OEmail=row["OfficialEmail"].ToString();
_PEmail=row["PersonalEmail"].ToString();
_OAStreet = row["OAStreet"].ToString();
_OACity = row["OACity"].ToString();
_OAState = row["OAState"].ToString();
_OACountry =row["OACountry"].ToString();
_OAZipCode = row["OAZip"].ToString();
_PAStreet = row["PAStreet"].ToString();
_PACity = row["PACity"].ToString();
_PAState = row["PAState"].ToString();
_PACountry = row["PACountry"].ToString();
_PAZipCode = row["PAZip"].ToString();
}
}
}
Insert form calling above function from Business Layer:
private void btnSave_Click(object sender, System.EventArgs e)
{
string Title = drplstTitle.SelectedItem.Text;
string FirstName = txtFirstName.Text;
string MiddleName = txtMiddleName.Text;
string LastName = txtLastName.Text;
string JobTitle = this.txtJobTitle.Text;
string Company = this.txtCompany.Text;
string Website = this.txtWebSite.Text;
string OfficePhone = this.txtOfficePhone.Text;
string HomePhone = this.txtHomePhone.Text;
string Mobile = this.txtMobile.Text;
string Fax = this.txtFax.Text;
string OEmail = this.txtOfficialEmail.Text;
string PEmail = this.txtPersonalEmail.Text;
string OAStreet = this.txtOAStreet.Text;
string OACity = this.txtOACity.Text ;
string OAState = this.txtOAState.Text;
string OACountry = this.txtOACountry.Text;
string OAZipCode = this.txtOAZipCode.Text;
string PAStreet = this.txtPAStreet.Text;
string PACity = this.txtPACity.Text;
string PAState = this.txtPAState.Text;
string PACountry = this.txtPACountry.Text;
string PAZipCode = this.txtPAZipCode.Text;
aspdotnet.BusinessLogicLayer.ContactEntry AddEntry =
new ContactEntry(Convert.ToInt32(Session["ContactID"].ToString()),Title,FirstName,MiddleName,LastName,JobTitle,Company,Website,OfficePhone,HomePhone,Mobile,Fax,OEmail,PEmail,OAStreet,OACity,OAState,OACountry,OAZipCode,PAStreet,PACity,PAState,PACountry,PAZipCode);
//AddEntry.Save();
}
I get object reference not set to an insance of object right after above method aspdotnet.BusinessLogicLayer.ContactEntry above. I see all values are being passed when I am debugging. I entered all values when entering values above but it still errors out. Not sure what I am missing. Please help , appreciate your time.
This line is suspicious:
Convert.ToInt32(Session["ContactID"].ToString())
Session["ContactID"] may return null and calling ToString() on null blows up. Convert.ToInt32(null) wouldn't throw an error - it would return 0, not NULL.
You to handle Session["ContactID"] for nulls and Empty before Converting it to Int32
Example :
ContactEntry cEntry = null;
if(Session["ContactID"] != null)
{
if(!String.IsNullOrEmpty(Session["ContactID"]))
{
cEntry = new ContactEntry(Convert.ToInt32(Session["ContactID"].ToString());
}
}
Check your Session["ContactID"].
Consider changing your code to something like this...
//...
string PAState = this.txtPAState.Text;
string PACountry = this.txtPACountry.Text;
string PAZipCode = this.txtPAZipCode.Text;
string contactIDstr = Session["ContactID"] as string;
int contactID = 0;
if(!String.IsNullOrEmpty(conactIDStr))
Int32.TryParse(contactIDStr, out contactID);
aspdotnet.BusinessLogicLayer.ContactEntry AddEntry = new ContactEntry(
contentID,
Title,
FirstName,MiddleName,LastName,JobTitle,Company,
Website,OfficePhone,HomePhone,Mobile,Fax,OEmail,PEmail,
OAStreet,OACity,OAState,OACountry,OAZipCode,PAStreet,PACity,PAState,PACountry,PAZipCode
);
After filling the form when i am clicking on submit button...nothing is happening i mean no events are performed. Pls help me... here is my code..
PatientProperty.cs
public class PatientProperty
{
private string Pdisease;
private string Pname;
private string Pcategory;
private string Paddr;
private DateTime Dateofjoining;
private int Page;
public string PNAME
{
get
{
return Pname;
}
set
{
Pname = value;
}
}
public string PADDRESS
{
get
{
return Paddr;
}
set
{
Paddr = value;
}
}
public int PAGE
{
get
{
return Page;
}
set
{
Page = value;
}
}
public string PDISEASE
{
get
{
return Pdisease;
}
set
{
Pdisease = value;
}
}
public string PCATEGORY
{
get
{
return Pcategory;
}
set
{
Pcategory = value;
}
}
public DateTime DATEOFJOINING
{
get
{
return Dateofjoining;
}
set
{
Dateofjoining = value;
}
}
}
PatientRegistration.cs
public class PatientRegistration
{
string str = ConfigurationManager.ConnectionStrings["HealthCare"].ConnectionString.ToString();
public void InsertPatient(PatientProperty obj)
{
using (var con = new SqlConnection(str))
{
using (var com = new SqlCommand("PatientRegister", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("Pname", obj.PNAME);
com.Parameters.AddWithValue("Paddr", obj.PADDRESS);
com.Parameters.AddWithValue("Page", obj.PAGE);
com.Parameters.AddWithValue("Pdisease", obj.PDISEASE);
com.Parameters.AddWithValue("Pcategory", obj.PCATEGORY);
com.Parameters.AddWithValue("Dateofjoining", obj.DATEOFJOINING); con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
}
}
PatientRegistrationBussiness.cs
public class PatientRegistrationBussiness
{
public void AddPatient(PatientProperty obj)
{
PatientRegistration PR = new PatientRegistration();
PR.InsertPatient(obj);
}
}
protected void Button1_Click(object sender, System.EventArgs e)
{
string name = TextBox2.Text;
string address = TextBox3.Text;
string category = RadioButtonList1.Text;
int age =Convert.ToInt32(TextBox4.Text);
string disease = TextBox5.Text;
DateTime date =Convert.ToDateTime(TextBox6.Text);
PatientRegistrationBussiness obj = new PatientRegistrationBussiness();
try
{
PatientProperty PP = new PatientProperty();
PP.PNAME = name;
PP.PADDRESS = address;
PP.PAGE = age;
PP.PDISEASE = disease;
PP.PCATEGORY = category.ToString();
PP.DATEOFJOINING = date;
obj.AddPatient(PP);
Response.Write("Patient details have been successfully added");
TextBox2.Text = string.Empty;
TextBox3.Text = string.Empty;
TextBox4.Text = string.Empty;
TextBox5.Text = string.Empty;
TextBox6.Text = string.Empty;
RadioButtonList1.SelectedIndex = 0;
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
obj = null;
}
}
You have to hook up your events e.g. this.Load += new EventHandler(Page_Load); unless you set the AutoEventWireup="true".
Try putting a breakpoint in the Button1_Click method. In particular, I expect that an exception is being thrown and lost:
catch (Exception ex)
{
ex.Message.ToString();
}
does nothing! Either show the message to the user, or log it somewherre (perhaps Trace.WriteLine(ex)).
Also - don't set obj to null - there is no purpose.