Hi All !!
It seems everyone in this forum works on Web Applications and I haven't found anyone helping out with Windows Applications . That's a pity.
I am working on a Windows Application where I have a Parent Windows Form which opens a Popup Window with a button click. In the Popup window there is a Listbox where I can choose a value & click OK.
With the OK button click event I have written code which picks up values from database depending on the value selected from the Listbox. Now after closing the Popup window with the OK button the values picked from the databases have to populate the Texbox controls of the Parent Windows Form
The code I have written works but with a Hitch. Every time I close the Popup window a new Parent Window is opened with the Textbox Controls populated and the original Parent Window remains open without any controls populated.
I am putting the codes from both the forms here. Kindly let know where I might have gone wrong. Remember there is no problem in retrieving values from Popup.
Code for Parent Window :
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Web;
namespace QuizPro
{
public partial class frmQuizName : Form
{
public frmQuizName()
{
InitializeComponent();
}
public frmQuizName(string strQid, string strQName)
{
InitializeComponent();
this.txbQuizid.Text = strQid;
this.txtQuizName.Text = strQName;
}
private void frmQuizName_Load(object sender, EventArgs e)
{
btnDelete.Visible = false;
}
private void btnSaveEdit_Click(object sender, EventArgs e)
{
if (txbQuizid.Text == "")
{
btnSaveEdit.Text = "&Save";
InsertRecords();
Refresh();
}
else
{
btnSaveEdit.Text = "&Edit";
EditRecords();
}
}
private void InsertRecords()
{
try
{
string newDt = Convert.ToString(DateTime.Now.Day) +"/" + Convert.ToString(DateTime.Now.Month) + "/" + Convert.ToString(DateTime.Now.Year);
OleDbConnection conn = TheConnection();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
string qid = getQuizID();
cmd.CommandText = "Insert into Quiz_Master(QuizID,Quiz_Name,CreatDt) values('" + qid + "','" + txtQuizName.Text + "','" + newDt + "')";
int recCount = cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted Successfully");
conn.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void EditRecords()
{
}
private string getQuizID()
{
string QuzId = string.Empty;
OleDbConnection newcon = TheConnection();
OleDbCommand newcmd = new OleDbCommand();
newcmd.Connection = newcon;
OleDbDataAdapter dadpter = new OleDbDataAdapter("Select Count(*) as RecCount from Quiz_Master",newcon);
DataSet ds = new DataSet();
dadpter.Fill(ds, "Quiz_Master");
DataTable dt = ds.Tables[0];
if (dt.Rows[0]["RecCount"].ToString() != string.Empty)
{
if (dt.Rows[0]["RecCount"].ToString().Length == 1)
{
QuzId = "Q" + "000" + Convert.ToString(Convert.ToInt32(Convert.ToInt32(Convert.ToString(dt.Rows[0]["RecCount"])) + 1));
}
else if (dt.Rows[0]["RecCount"].ToString().Length == 2)
{
QuzId = "Q" + "00" + Convert.ToString(Convert.ToInt32(Convert.ToInt32(Convert.ToString(dt.Rows[0]["RecCount"])) + 1));
}
else if (dt.Rows[0]["RecCount"].ToString().Length == 3)
{
QuzId = "Q" + "0" + Convert.ToString(Convert.ToInt32(Convert.ToInt32(Convert.ToString(dt.Rows[0]["RecCount"])) + 1));
}
else if (dt.Rows[0]["RecCount"].ToString().Length == 4)
{
QuzId = "Q" + Convert.ToString(Convert.ToInt32(Convert.ToInt32(Convert.ToString(dt.Rows[0]["RecCount"])) + 1));
}
}
else
{
QuzId = "Q" + "0001";
}
return QuzId;
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void btnShowQuiz_Click(object sender, EventArgs e)
{
ShowQuiz frmchild = new ShowQuiz();
frmchild.Width = 480;
frmchild.Height = 290;
frmchild.Top = 65;
frmchild.Left = 350;
frmchild.Show();
}
private OleDbConnection TheConnection()
{
OleDbConnection conn = new OleDbConnection();
string Connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Quiz Software\\QuizDB.mdb";
conn.ConnectionString = Connstring;
conn.Open();
return conn;
}
}
}
Highlighted code shows the relevant code area.
Popup page Code:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace QuizPro
{
public partial class ShowQuiz : Form
{
public ShowQuiz()
{
InitializeComponent();
}
private void ShowQuiz_Load(object sender, EventArgs e)
{
try
{
DateTime newDt = DateTime.Now;
OleDbConnection newcon = TheConnection();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = newcon;
cmd.CommandText = "Select QuizID,Quiz_Name from Quiz_Master order by QuizID";
OleDbDataReader dr = cmd.ExecuteReader();
lstQuizName.Items.Clear();
lstQuizName.BeginUpdate();
while (dr.Read())
{
lstQuizName.Items.Add(dr.GetString(1));
}
lstQuizName.EndUpdate();
dr.Close();
newcon.Close();
}
catch
{
}
}
private OleDbConnection TheConnection()
{
OleDbConnection conn = new OleDbConnection();
string Connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Quiz Software\\QuizDB.mdb";
conn.ConnectionString = Connstring;
conn.Open();
return conn;
}
private void btnOK_Click(object sender, EventArgs e)
{
string aa="";
string selctdVal = lstQuizName.SelectedItem.ToString();
OleDbConnection dbconn = TheConnection();
OleDbDataAdapter dadpter = new OleDbDataAdapter("Select QuizID from Quiz_Master where Quiz_Name = '" + selctdVal.Trim() + "'", dbconn);
DataSet ds = new DataSet();
dadpter.Fill(ds, "Quiz_Master");
DataTable dt = ds.Tables[0];
if(dt.Rows[0]["QuizID"].ToString() != string.Empty)
{
aa = dt.Rows[0]["QuizID"].ToString();
}
frmQuizName ff = new frmQuizName(aa, selctdVal);
ff.Show();
Close();
}
private void btncancel_Click(object sender, EventArgs e)
{
Close();
}
}
}
Any help in this regard will be appreciated. I am not familiar with Windows application..
Thanks in advance.