Hiii,
I am trying to make dynamic interface for my application.
I have lots of 2 field database table.i.e (ID,Name)
For that doing each table each form i am trying to make it simple by making class file and
passing query string parameter to li (Link of form) and then access it on page.
accoridng to query string value i am making further operations.
BUt value assign to variable on page load vanished on button click.
I am also want to bind gridview on same way.
//Method for Auto Increment(ID)
public int Increment_ID(string _tblName, string FieldId)
{
int _MaxID = 0;
try
{
using (OleDbConnection VMScon = new OleDbConnection(VMSConnectionString))
{
OleDbCommand cmdVcReqId = new OleDbCommand("select nvl(max(" + FieldId + "),0) from " + _tblName + "", VMScon);
// 0 used to get value if there is no data found in table.
VMScon.Open();
_MaxID = Convert.ToInt16(cmdVcReqId.ExecuteScalar());
_MaxID = _MaxID + 1;
VMScon.Close();
}
}
catch (Exception ex)
{
//AlertMsg.Attributes["class"] = "alert alert-danger";
//lblmsg.Text = "Error Occured at Auto ID!!";
//mpeMessageBox.Show();
}
return _MaxID;
}
public DataSet GetMasterData(string tablename, int Id)
{
//DataTable dt = new DataTable();
DataSet ds = new DataSet();
OleDbConnection conn = new OleDbConnection(VMSConnectionString);
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
conn.Open();
string cmdText = "SELECT * FROM " + tablename + " order by " + Id + " desc";
OleDbCommand cmd = new OleDbCommand(cmdText, conn);
cmd.Connection = conn;
da.SelectCommand = cmd;
da.Fill(ds);
}
catch (Exception e)
{
if (WriteExceptionsToEventLog)
{
WriteToEventLog(e, "GetTasksForSelectedUser");
}
throw e;
}
finally
{
conn.Close();
}
return ds;
}
public int IsExistMasterData(string tablename,string FieldName,string description)
{
using (OleDbConnection VMScon = new OleDbConnection(VMSConnectionString))
{
int result = 0;
try
{
TextInfo objtxt = culterinfo.TextInfo;
_description = objtxt.ToUpper(description);
VMScon.Open();
OleDbCommand cmd = new OleDbCommand("Select * from "+tablename+" where "+FieldName+"=:description ", VMScon);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
result = 1;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
VMScon.Close();
}
return result;
}
}
/// <summary>
/// Add new New Current News in the PIS database
/// </summary>
/// <param name="News"></param>
/// <returns>The number of Newss created</returns>
public void AddMasterData(string tableName, string FieldId,string FieldName,string description)
{
using (OleDbConnection VMScon = new OleDbConnection(VMSConnectionString))
{
try
{
TextInfo objtxt = culterinfo.TextInfo;
_maxId = Increment_ID(tableName, FieldId);
_description = objtxt.ToUpper(description);
string queryStr = "insert into " + tableName + " (" + FieldId + "," + FieldName + ")values(:ID,:Description)";
VMScon.Open();
OleDbCommand cmd = new OleDbCommand(queryStr, VMScon);
cmd.Parameters.Add(new OleDbParameter(":ID", _maxId));
cmd.Parameters.Add(new OleDbParameter(":Description", _description));
cmd.ExecuteNonQuery();
VMScon.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
VMScon.Close();
}
}
}
public void UpdateMasterData(string tableName, string FieldId, string FieldName,int Id, string description)
{
using (OleDbConnection VMScon = new OleDbConnection(VMSConnectionString))
{
try
{
TextInfo objtxt = culterinfo.TextInfo;
_description = objtxt.ToUpper(description);
string queryStr = "update " + tableName + " set " + FieldName + "=:description where " + FieldId + "=:ID";
VMScon.Open();
OleDbCommand cmd = new OleDbCommand(queryStr, VMScon);
cmd.Parameters.Add(new OleDbParameter(":description", _description));
cmd.Parameters.Add(new OleDbParameter(":FieldId", Id));
cmd.ExecuteNonQuery();
VMScon.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
VMScon.Close();
}
}
}
string tableName = "";
string columnId = "";
string columnName = "";
string description = "";
int result = 0;
DbHelper db;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//string FormID = Request.QueryString["FormValue"].ToString();
string FormID = "1";
if (FormID != null)
{
switch (FormID)
{
case "1":
tableName = "Mst_Country";
columnId = "Country_Id";
columnName = "Country_Name";
this.lblFieldName.Text = "Country Name";
break;
default:
break;
}
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
result = db.AddMasterData(tableName, columnId, columnName, txtFieldName.Text);
if (result > 0)
{
lblmsg.Text = "Record Saved Successfully !!!";
mpeMessageBox.Show();
}
}