i am trying to insert the value from control into xml but the record is getting overwriting with the previous one that is only one entry remains in xml file.plz give me solution,my code is like:
namespace StudentApplicationForm
{
public class information
{
private String txtbox1;
private String txtbox2;
private String txtbox3;
public String StudentName
{
get { return txtbox1; }
set{ txtbox1 = value; }
}
public String StudentId
{
get { return txtbox2; }
set{ txtbox2 = value; }
}
public String StudentBranch
{
get { return txtbox3; }
set { txtbox3 = value; }
}
}
}//getter and setter methods
and the file actual insert logic is written is:
public void ok_Click(object sender, EventArgs e)
{
try
{
information info = new information();
List<information> i1 = new List<information>();
XmlSerializer serial = new XmlSerializer(typeof(List<information>));
info.StudentName = textBox1.Text;//id of control
info.StudentId = textBox2.Text;
if (radioButton1.Checked)
{
info.StudentBranch = radioButton1.Text;
}
if (radioButton2.Checked)
{
info.StudentBranch = radioButton2.Text;
}
if (radioButton3.Checked)
{
info.StudentBranch = radioButton3.Text;
}
i1.Add(info);
using (FileStream fs = newFileStream(Environment.CurrentDirectory + "\\mynew.xml", FileMode.Create, FileAccess.Write))
{
serial.Serialize(fs, i1);
MessageBox.Show("Created");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Are you tried like this,
i1.Add(new information{ StudentName = info.StudentName, StudentId = info.StudentId, StudentBranch = info.StudentBranch});
I have:
private void Tab2KsiazkiBTSzczegoly_Click(object sender, EventArgs e)
{
string KodKsiazki;
KodKsiazki = DataWyszukajKsiazki.Rows[DataWyszukajKsiazki.CurrentCell.RowIndex].Cells[2].Value.ToString();
TSzczegolyDb _szczegoly = new TSzczegolyDb();
Global.listSzczegoly = _szczegoly.GetSZCZEGOLY(KodKsiazki);
//StringBuilder sb = new StringBuilder();
//foreach (DataGridViewCell cell in DataWyszukajKsiazki.SelectedCells)
//{
// sb.AppendLine(cell.Value.ToString());
//}
//MessageBox.Show(sb.ToString());
//}
MessageBox.Show(_szczegoly.ToString());
}
class like that:
public class TSzczegolyDb : Core.CoreMSSQL
{
static string connectionString = TconStrDb.GetConectionString();
public TSzczegolyDb()
: base(connectionString)
{
}
public List<TSzczegolyDto> GetSZCZEGOLY(string co)
{
List<TSzczegolyDto> list = null;
list = new List<TSzczegolyDto>();
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT Tytul, Autorzy, ISBN10, ISBN13, IlStron, Wydawnictwo, Gatunek, Opis FROM dbo.TKsiazki WHERE dbo.TKsiazki.KodKsiazki = '" + co + "'";
SqlDataReader reader = ExecuteQuery(command);
while (reader.Read())
{
TSzczegolyDto message = new TSzczegolyDto();
if (!reader.IsDBNull(0))
{
message.Tytuł = reader.GetString(0);
}
if (!reader.IsDBNull(1))
{
message.Autorzy = reader.GetString(1);
}
if (!reader.IsDBNull(2))
{
message.ISBN10 = reader.GetString(2);
}
if (!reader.IsDBNull(3))
{
message.ISBN13 = reader.GetString(3);
}
if (!reader.IsDBNull(4))
{
message.IlStron = reader.GetInt32(4);
}
if (!reader.IsDBNull(5))
{
message.Wydawnictwo = reader.GetString(5);
}
if (!reader.IsDBNull(6))
{
message.Gatunek = reader.GetString(6);
}
if (!reader.IsDBNull(7))
{
message.Opis = reader.GetString(7);
}
list.Add(message);
}
return list;
}
and second:
public class TSzczegolyDto
{
private string _tytul;
public string Tytuł
{
get { return _tytul; }
set { _tytul = value; }
}
private string _autorzy;
public string Autorzy
{
get { return _autorzy; }
set { _autorzy = value; }
}
private string _ISBN10;
public string ISBN10
{
get { return _ISBN10; }
set { _ISBN10 = value; }
}
private string _ISBN13;
public string ISBN13
{
get { return _ISBN13; }
set { _ISBN13 = value; }
}
private long _ilstron;
public long IlStron
{
get { return _ilstron; }
set { _ilstron = value; }
}
private string _wydawnictwo;
public string Wydawnictwo
{
get { return _wydawnictwo; }
set { _wydawnictwo = value; }
}
private string _gatunek;
public string Gatunek
{
get { return _gatunek; }
set { _gatunek = value; }
}
private string _opis;
public string Opis
{
get { return _opis; }
set { _opis = value; }
}
}
I want show _szczegoly on MessageBox but when I try to MessageBox.Show(_szczegoly.ToString()); then is wrong. In _szczegoly I have string and long type data.
How to create messagebox with this data?
I think you are trying to show an Object with a MessageBox, you need to override the ToString() method to show propertly:
class TSzczegolyDb
{
public override string ToString()
{
return this.Property1 + this.Property2 /*....*/;
}
}
I want to load values from database in user control
I could add more user control but I cant't able to load values in user control
Code:
Add.aspx.cs
Below code is for adding more than one user control and retain the previous control values
public List<string> NoOfControls
{
get
{
return ViewState["NoOfControls"] == null ? new List<string>() : (List<string>)ViewState["NoOfControls"];
}
set
{
ViewState["NoOfControls"] = value;
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
GenerateControls();
}
private void GenerateControls()
{
foreach (string i in NoOfControls)
{
VisaUserControl ctrl = (VisaUserControl)Page.LoadControl("VisaUserControl.ascx");
ctrl.ID = i;
this.rpt1.Controls.Add(ctrl);
}
}
protected void btnAddVisa_Click(object sender, EventArgs e)
{
List<string> temp = null;
var uc = (VisaUserControl)this.LoadControl(#"VisaUserControl.ascx");
string id = Guid.NewGuid().ToString();
uc.ID = id;
temp = NoOfControls;
temp.Add(id);
NoOfControls = temp;
rpt1.Controls.Add(uc);
}
Edit.aspx.cs
Below code is for loading values from database in user control
using (OleDbCommand cmd = new OleDbCommand("Select * from visa_details where emp_id = '"+ empid +"'", DbConnection))
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
OleDbDataReader DR1 = cmd.ExecuteReader();
while(DR1.Read())
{
//Here I can get values
string visaNumb = DR1[2].ToString();
string visaCountry = DR1[3].ToString();
string visaType = DR1[4].ToString();
string visaEntry = DR1[5].ToString();
string expiryDate = DR1[6].ToString();
for (int i = 0; i < y; i++)
{
VisaUserControl userconrol = (VisaUserControl)Page.LoadControl("VisaUserControl.ascx");
userconrol.TextVisaNumber = visaNumb;
userconrol.VisaCountry = visaCountry;
userconrol.VisaType = visaType;
userconrol.VisaEntry = visaEntry;
userconrol.ExpiryDate = expiryDate;
repeater1.Controls.Add(userconrol);
}
}
}
.ascx.cs
Here values are there but when it is loading all values are removed
protected void Page_Load(object sender, EventArgs e)
{
txtUser.Text = Request.Form[txtUser.UniqueID];
dropCountry.SelectedValue = Request.Form[dropCountry.UniqueID];
dropVisa.SelectedValue = Request.Form[dropVisa.UniqueID];
dropEntry.SelectedValue = Request.Form[dropEntry.UniqueID];
txtDate.Text = Request.Form[txtDate.UniqueID];
}
public string TextVisaNumber
{
get { return txtUser.Text; }
set { txtUser.Text = value; }
}
public string VisaCountry
{
get { return dropCountry.SelectedValue; }
set { dropCountry.SelectedValue = value; }
}
public string VisaType
{
get { return dropVisa.SelectedValue; }
set { dropVisa.SelectedValue = value; }
}
public string VisaEntry
{
get { return dropEntry.SelectedValue; }
set { dropEntry.SelectedValue = value; }
}
public string ExpiryDate
{
get
{
return txtDate.Text;
}
set
{
txtDate.Text = value;
}
}
Any ideas? Thanks in advance
Instead of adding the control in the page's controls collection, you should keep an set the data in some existing control's inner HTML.
I'm pretty new to programming and have run into a problem with a basic application I am working on. I have a person class which resembles this...
Person
{
SqlConnection conn = new SqlConnection(#"Integrated Security=True; Data
Source=ME\MyPRESS;Initial Catalog=TEST5");
SqlCommand cmd = new SqlCommand("usp_employee", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#emp_id", SqlDbType.Int).Value = id;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
try
{
conn.Open();
{
department = dr["dept_name"].ToString();
fname = dr["emp_first_name"].ToString();
lname = dr["emp_last_name"].ToString();
email = dr["emp_email"].ToString();
phone = dr["emp_phone"].ToString();
position = dr["emp_position"].ToString();
address1 = dr["emp_address1"].ToString();
address2 = dr["emp_address2"].ToString();
city = dr["emp_city"].ToString();
state = dr["emp_state"].ToString();
postal_code = dr["emp_postal_code"].ToString();
// department = txtFirst_name.Text;
}
}
finally
{
// 3. close the reader
if (dr != null)
{
dr.Close();
}
// close the connection
if (conn != null)
{
conn.Close();
}
}
}
protected string department;
public string Department
{
get { return department; }
set { department = value; }
}
protected string fname;
public string Fname
{
get { return fname; }
set { fname = value;}
}
protected string lname;
public string Lname
{
get { return lname; }
set { lname = value; }
}
protected string email;
public string Email
{
get { return email; }
set { email = value; }
}
protected string position;
public string Position
{
get { return position; }
set { position = value; }
}
protected string address1;
public string Address1
{
get { return address1; }
set { address1 = value; }
}
protected string address2;
public string Address2
{
get { return address2; }
set { address2 = value; }
}
protected string phone;
public string Phone
{
get { return phone; }
set { phone = value; }
}
protected string city;
public string City
{
get { return city; }
set { city = value; }
}
protected string state;
public string State
{
get { return state; }
set { state = value; }
}
protected string postal_code;
public string Postal_Code
{
get { return postal_code; }
set { postal_code = value; }
}
}
I have a form and a db with a stored procedure to retrieve the data as you can see above....
Where I am lost is in wiring up my form to display the record from the stored procedure.
A simple way would be to define a constructor for your Person class:
public class Person
{
public Person(string Department, string fName /* etc */)
{
this.Department = Department;
this.fName = fName;
// and so on for all the fields
}
// the rest of the class definition goes here
}
Then when looping through the data, create a Person object using that constructor. Return the new Person object from your function.
That helps! I have it up and running now....
public Person(int id)
{
SqlConnection conn = new SqlConnection(#"Integrated Security=True; Data Source=JODIEPC\XPRESS;Initial Catalog=TEST5");
conn.Open();
SqlCommand cmd = new SqlCommand("usp_EmployeeSelect", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#emp_id", SqlDbType.Int).Value = id;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
department = dr["dept_name"].ToString();
fname = dr["emp_first_name"].ToString();
lname = dr["emp_last_name"].ToString();
email = dr["emp_email"].ToString();
phone = dr["emp_phone"].ToString();
position = dr["emp_position"].ToString();
address1 = dr["emp_address1"].ToString();
address2 = dr["emp_address2"].ToString();
city = dr["emp_city"].ToString();
state = dr["emp_state"].ToString();
postal_code = dr["emp_postal_code"].ToString();
}
}
protected int id;
protected string fname;
protected string lname;
protected string department;
protected string email;
protected string position;
protected string address1;
protected string address2;
protected string phone;
protected string city;
protected string state;
protected string postal_code;
public int Id
{
get { return id; }
set { id = value; }
}
public string Department
{
get { return department; }
set{ department = value; }
}
public string Fname
{
get { return fname; }
set { fname = value; }
}
public string Lname
{
get { return lname; }
set { lname = value; }
}
public string Email
{
get { return email; }
set { email = value; }
}
public string Position
{
get { return position; }
set { position = value; }
}
public string Address1
{
get { return address1; }
set { address1 = value; }
}
public string Address2
{
get { return address2; }
set { address2 = value; }
}
public string Phone
{
get { return phone; }
set { phone = value; }
}
public string City
{
get { return city; }
set { city = value; }
}
public string State
{
get { return state; }
set { state = value; }
}
public string Postal_Code
{
get { return postal_code; }
set { postal_code = value; }
}
}
---------winform---------------
private void btnSearch_Click(object sender, EventArgs e)
{
Person Emp = new Person(Int32.Parse(txtId.Text));
txtFirstName.Text = Emp.Fname;
txtLastName.Text = Emp.Lname;
txtEmail.Text = Emp.Fname;
txtPhone.Text = Emp.Phone;
txtDepartment.Text = Emp.Department;
txtPosition.Text = Emp.Position;
txtAddress1.Text = Emp.Address1;
txtAddress2.Text =Emp.Address2;
txtCity.Text = Emp.City;
txtState.Text = Emp.State;
txtPostalCode.Text = Emp.Postal_Code;
}
}
It's not the most creative way of going about it but it works....
Thanks alot guys....
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
);