Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > WinForms/Console Application Design
| Search | Today's Posts | Mark Forums Read
Welcome to the p2p.wrox.com Forums.

You are currently viewing the WinForms/Console Application Design section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 18th, 2009, 12:51 AM
Friend of Wrox
Points: 686, Level: 9
Points: 686, Level: 9 Points: 686, Level: 9 Points: 686, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2005
Location: Kolkata, West Bengal, India.
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting values from Popup Window to Parent Window

Hi All !!

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 but the original Parent Window remains open without any controls being populated.

I am putting the codes from both the forms here. Kindly let me 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 very familiar with Windows application..

Thanks in advance.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending values from Popup to Parent window arnabghosh ASP.NET 3.5 Professionals 1 October 17th, 2009 06:17 AM
Getting values from Popupo window to parent form arnabghosh ASP.NET 3.5 Professionals 0 October 16th, 2009 01:00 PM
Problem in closing the parent window from a popup rakesh.kashnia1 Javascript 4 July 9th, 2007 05:39 AM
Centering Popup window & Blur Parent window jkusmanto Javascript How-To 0 May 25th, 2007 03:19 AM
refresh the parent window when closing the popup ram_siddinen VS.NET 2002/2003 1 January 30th, 2004 04:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.