THEFUN CHAKTHI

THEFUN CHAKTHI

  • NA
  • 26
  • 6k

Second entry gives data missing error

Apr 2 2014 10:50 AM
Hi... I tried a project add school detail first entry entered without problem. But from second entry and update of the first entry Gives error as data type missing . please help meto overcome this.here is my code :


public Trfrm()
{
InitializeComponent();

}
public class ComboboxItem
{

// Properties
public string Text { get; set; }

public object Value { get; set; }
}


private void Trfrm_Load(object sender, EventArgs e)
{

{


this.LFLoadComboBox(this.cbodist, "District");
this.LFLoadComboBox(this.cboclass, "Class");
this.LFLoadComboBox(this.cbosection, "Section");
this.LFLoadComboBox(this.cboyear, "Term");
try
{
OleDbConnection connection = new OleDbConnection(Global.DBMSTCONSTR);
connection.Open();
string cmdText = "Select DIST,DISTNAME FROM DISTRICT ORDER BY DISTNAME";
OleDbCommand selectCommand = new OleDbCommand(cmdText, connection);
OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
List<ComboboxItem> list = new List<ComboboxItem>();
ComboboxItem item = new ComboboxItem {
Text = "-Select-",
Value = "-1"
};
list.Add(item);
for (int i = 0; i < dataTable.Rows.Count; i++)
{
item = new ComboboxItem {
Text = dataTable.Rows[i][1].ToString(),
Value = dataTable.Rows[i][0].ToString()
};
list.Add(item);
}
this.cbodist.DisplayMember = "Text";
this.cbodist.ValueMember = "Value";
this.cbodist.DataSource = list;
this.LFLoadSchools();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}

}

private bool LFCheckDataFile(string schid, bool CreateFile)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\CCE\DATA\" + schid + @"\";
if (!Directory.Exists(path))
{
if (!CreateFile)
{
return false;
}
Directory.CreateDirectory(path);
}

string str2 = Application.StartupPath + @"\temp.mdb";
if (!File.Exists(str2))
{
MessageBox.Show("Database file not found !");
return false;
}
path = path + @"\" + schid + ".mdb";
if (!File.Exists(path))
{
if (!CreateFile)
{
return false;
}
File.Copy(str2, path);
}
Global.DBCONSTR = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";";
return true;
}

private void LFLoadComboBox(ComboBox cbobox, string Opt)
{
cbobox.DisplayMember = "Text";
cbobox.ValueMember = "Value";

List<ComboboxItem> list = new List<ComboboxItem>();
ComboboxItem item = new ComboboxItem
{
Text = "-Select-",
Value = "-1"

};
list.Add(item);


if (Opt == "Class")
{
item = new ComboboxItem
{
Text = "VI",
Value = "6"

};
list.Add(item);
item = new ComboboxItem
{
Text = "VII",
Value = "7"

};
list.Add(item);

item = new ComboboxItem
{
Text = "VIII",
Value = "8"

};
list.Add(item);
cbobox.DataSource = list;
}

if (Opt == "Section")
{
item = new ComboboxItem
{
Text = "A",
Value = "A"

};
list.Add(item);
item = new ComboboxItem
{
Text = "B",
Value = "B"

};
list.Add(item);

item = new ComboboxItem
{
Text = "C",
Value = "C"

};
list.Add(item);

cbobox.DataSource = list;
}
if (Opt == "Term")
{
item = new ComboboxItem
{
Text = "Term I",
Value = "I"

};
list.Add(item);
item = new ComboboxItem
{
Text = "Term II",
Value = "II"

};
list.Add(item);
item = new ComboboxItem
{
Text = "Term III",
Value = "III"

};
list.Add(item);

cbobox.DataSource = list;



}
}

private void LFLoadSchools()
{
try
{
OleDbConnection connection = new OleDbConnection(Global.DBMSTCONSTR);
connection.Open();
string cmdText = "Select c.edu_distid as [Edu Dist ID],d.DISTNAME AS [Education District],c.schoolname as [School Name],c.place as [School Place],c.class as [Student Class],c.[section] as [Section],c.term as [Term],c.students as [Student Count],id from classinfo c , district d where c.edu_distid=d.dist";
OleDbCommand selectCommand = new OleDbCommand(cmdText, connection);
OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand);
selectCommand.CommandText = cmdText;
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
if (dataTable.Rows.Count == 0)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\CCE\DATA\";
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
}
else
{
this.dgvList.DataSource = dataTable;
this.dgvList.Columns[0].Width = 150;
this.dgvList.Columns[1].Visible = false;
this.dgvList.Columns[2].Width = 150;
this.dgvList.Columns[3].Visible = false;
this.dgvList.Columns[4].Width = 170;
this.dgvList.Columns[5].Width = 170;
this.dgvList.Columns[6].Visible = false;
this.dgvList.Columns[7].Width = 70;
this.dgvList.Columns[8].Visible = false;

}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}

private void LFSave()
{
this.totsc.Text = this.totsc.Text.Trim();
this.Schname.Text = this.Schname.Text.Trim().Replace("'", "").ToUpper();
this.Placename.Text = this.Placename.Text.Trim();
if (this.cbodist.SelectedIndex == 0)
{
MessageBox.Show("Select the Education District !");
this.cbodist.Focus();
}
else if (this.Schname.Text.Length == 0)
{
MessageBox.Show("Enter the School Name !");
this.Schname.Focus();
}

else if (this.Placename.Text.Length == 0)
{
MessageBox.Show("Enter the Place !");
this.Placename.Focus();
}
else if (this.cboclass.SelectedIndex == 0)
{
MessageBox.Show("Enter the Class !");
this.cboclass.Focus();
}
else if (this.cbosection.SelectedIndex == 0)
{
MessageBox.Show("Select the Section !");
this.cbosection.Focus();
}
else if (this.cboyear.SelectedIndex == 0)
{
MessageBox.Show("Select the Term !");
this.cboyear.Focus();
}
else if (this.totsc.Text.Length == 0)
{
MessageBox.Show("Enter the Place !");
this.totsc.Focus();
}

else
{
string cmdText = "";
if (this.lsRecID == "")
{
cmdText = string.Concat(new object[] {
"INSERT INTO classinfo (EDU_DISTID,SCHOOLNAME,PLACE,CLASS,[SECTION],TERM,STUDENTS) VALUES ('", this.cbodist.SelectedValue, "','", this.Schname.Text, "','", this.Placename.Text, "','", this.cboclass.SelectedValue.ToString(), "','", this.cbosection.SelectedValue.ToString(), "','", this.cboyear.SelectedValue.ToString(), "',", this.totsc.Text,
")"

});
}
else
{
cmdText = string.Concat(new object[] { "update classinfo set EDU_DISTID='", this.cbodist.SelectedValue, "',SCHOOLNAME='", this.Schname.Text, "',PLACE='", this.Placename.Text, "',CLASS='", this.cboclass.SelectedValue.ToString(), "',[SECTION]=", this.cbosection.SelectedValue.ToString(), ", TERM='", this.cboyear.SelectedValue.ToString(), "', STUDENTS=", this.totsc.Text, " where id=", this.lsRecID });
}
try
{
OleDbConnection connection = new OleDbConnection(Global.DBMSTCONSTR);
connection.Open();
OleDbCommand command = new OleDbCommand();
command = new OleDbCommand(string.Concat(new object[] { "Select * from classinfo where id='", this.cbodist.SelectedValue, "' and schoolname='", this.Schname.Text, "' and class='", this.cboclass.SelectedValue.ToString(), "'" }), connection);
OleDbDataReader reader = command.ExecuteReader();
int num = 0;
if (reader.Read())
{
num++;
}
reader.Close();
if ((num > 0) && (this.lsRecID == ""))
{
MessageBox.Show("Entered School Name or School No are already exists !");
return;
}
if (num > 1)
{
MessageBox.Show("Entered School Name or School No are already exists !");
return;
}
command.CommandText = cmdText;
command.ExecuteNonQuery();
command.CommandText = "Select Max(ID) from classinfo";
this.lsRecID = Convert.ToString(command.ExecuteScalar());
connection.Close();
if (this.LFCheckDataFile(this.lsRecID, true))
{
OleDbConnection connection2 = new OleDbConnection(Global.DBCONSTR);
connection2.Open();
new OleDbCommand(cmdText, connection2).ExecuteNonQuery();
this.lsRecID = "";
}
else
{
MessageBox.Show("Error in Data File Creation !");
return;
}
this.cbodist.SelectedIndex = 0;
this.cboclass.SelectedIndex = 0;
this.cbosection.SelectedIndex = 0;
this.cboyear.SelectedIndex = 0;
this.Schname.Clear();
this.Placename.Clear();
this.totsc.Clear();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
return;
}
this.pnllist.Visible = true;
this.pnlinfo.Visible = false;
this.LFLoadSchools();
if (this.dgvList.Rows.Count == 1)
{
this.Addbtn_Click(null, null);

}
}
}


private void Savebtn_Click(object sender, EventArgs e)
{
this.LFSave();
}

private void Addbtn_Click(object sender, EventArgs e)
{
{
this.lsRecID = "";
this.cbodist.SelectedIndex = 0;
this.cboclass.SelectedIndex = 0;
this.cbosection.SelectedIndex = 0;
this.cboyear.SelectedIndex = 0;
this.Schname.Clear();
this.Placename.Clear();
this.totsc.Clear();
this.pnllist.Visible = false;
this.pnlinfo.Visible = true;
this.cboclass.Enabled = true;
this.cbodist.Focus();
}
}

private void Editbtn_Click(object sender, EventArgs e)
{

try
{
this.cbodist.SelectedValue = Convert.ToString(this.dgvList.SelectedRows[0].Cells[0].Value);
this.Schname.Text = Convert.ToString(this.dgvList.SelectedRows[0].Cells[2].Value);
this.Placename.Text = Convert.ToString(this.dgvList.SelectedRows[0].Cells[3].Value);
this.cboclass.SelectedValue = Convert.ToString(this.dgvList.SelectedRows[0].Cells[4].Value);
this.cbosection.SelectedValue = Convert.ToString(this.dgvList.SelectedRows[0].Cells[5].Value);
this.cboyear.SelectedValue = Convert.ToString(this.dgvList.SelectedRows[0].Cells[6].Value);

this.totsc.Text = Convert.ToString(this.dgvList.SelectedRows[0].Cells[7].Value);
this.lsRecID = Convert.ToString(this.dgvList.SelectedRows[0].Cells[8].Value);
this.pnllist.Visible = false;
this.pnlinfo.Visible = true;
this.cbodist.Focus();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}


}


}


}