Search Data on a Window form and display the result on second windows form

Raj.Trivedi
Posted by in C# category on for Beginner level | Points: 250 | Views : 2938 red flag

This article explains how to search data on one windows form and show the results on another windows form on the base of the search criteria. We will be using ADO.NET dataset property and making it public and private.

Objective


The objective of this article is to know how we can search data from one form and display result to another in windows from on the base of search description.

In ASP.NET you can do it with Session object but in Windows Application there is nothing like session.
But you can still do it with ADO.NET dataset object.

**************************** Creation of the Application ***************************

1.Open MS Visual Studio -> File -> New Project -> Windows Application -> 
2.Now by default you will get Form1.cs rename it to SearchStudent.
3.Now right click on the Project -> Add New Item -> Select App.Config File.
4.Now in the configuration section add Connection String. Check the Code Section for adding connection String in the code below.
5.Now come to first form i.e. SearchStudent.cs
6.Add a Textbox and a Button.
7.Now right click on the Project -> Add New Item -> Select Windows Form and name it as DisplayStudent.cs
8.Add 3 textbox for AdmissionNumber , StudentName ,RollNumber on DisplayStudent form.
9.Over here we will declare a Private dataset and public dataset such as dsStoreDetails and private dataset               dsGetDetails respectively and specify the get and the set for the private dataset as shown in the code below.
10.Now come back to the SearchStudent.cs form and double click the button to trigger the button click event and
write the code as shown below.
11.Check the code for DisplayStudent.cs in the code section.

*****************************Creation of Database and Table and Stored Procedure*************

1. create database DotNetFunda

2.
 CREATE TABLE [dbo].[StudentAdmission](
	[AdmissionNumber] [varchar](20) NULL,
	[StudentName] [varchar](20) NULL,
	[StudentRollNumber] [int] NULL)

3. 
create proc GetStudents
	(
	@AdmissionNumber varchar(20)
	)
	as
	begin
	select * from StudentAdmission where AdmissionNumber = @AdmissionNumber
	end

Using the code


We will be using a stored Proc for searching the student and display the result in the windows form.

1.We will input the admission number and click on search.
2.After clicking on search, the data from the database will be fetched and we will show the DisplayStudent.cs form.


Block of code should be set style as "Code" like below.
// Code for SearchStudent.cs
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.SqlClient;
using System.Configuration;
namespace Dotnet_funda
{
   
public partial class SearchStudent : Form
   
{
       
string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ConnectionString;
       
string desc = "0";
       
       
public SearchStudent()
       
{
           
InitializeComponent();
       
}

       
private void btnSearch_Click(object sender, EventArgs e)
       
{

           
SqlConnection sqlconn = new SqlConnection(strconn);
           
DisplayStudent frmDisplay = new DisplayStudent(); // creating the object of the form
            sqlconn
.Open();
           
SqlCommand cmdsearchstudent = new SqlCommand("GetStudents", sqlconn);
            cmdsearchstudent
.CommandType = CommandType.StoredProcedure;
           
SqlDataAdapter da = new SqlDataAdapter(cmdsearchstudent);
           
DataSet dsstudent = new DataSet();
            da
.SelectCommand.Parameters.AddWithValue("@AdmissionNumber", SqlDbType.VarChar).Value               = txtAdmissionNumber.Text.Trim();
            da
.Fill(dsstudent);
           
if (dsstudent.Tables[0].Rows.Count > 0)
           
{
                frmDisplay
.dsGetDetails = dsstudent; // over here we are accessing the public dataset declared in displaystudent.cs form
                frmDisplay
.Show();
           
}

           
       
}
   
}
}
//DisplayStudent.cs form
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;

namespace Dotnet_funda
{
   
public partial class DisplayStudent : Form
   
{
       
private DataSet dsStoreDetails; //declaring private dataset that will store details that will come into public dataset dsGetDetails from SearchStudent.cs

       
public DataSet dsGetDetails
       
{
           
get
           
{
               
return dsStoreDetails;
           
}
           
set
           
{
                dsStoreDetails
= value;
           
}
       
}
       
public DisplayStudent()
       
{
           
InitializeComponent();
       
}

       
private void DisplayStudent_Load(object sender, EventArgs e)
       
{
           
GetStudentsfromSearchForm(); // calling the function
       
}


       
private void GetStudentsfromSearchForm() // function to display student details i.e coming from SearchStudent form.
       
{
           
try
           
{
                xtxtAdmissionNumber
.Text = dsStoreDetails.Tables[0].Rows[0]["AdmissionNumber"].ToString();
                xtxtStudentName
.Text = dsStoreDetails.Tables[0].Rows[0]["StudentName"].ToString();
                xtxtRollNumber
.Text = dsStoreDetails.Tables[0].Rows[0]["StudentRollNumber"].ToString();
           
}
           
catch (Exception ex)
           
{
               
MessageBox.Show(ex.Message);
           
}
       
}
   
}
}
// app.config file
<?xml version="1.0"?>
<configuration>
 
<configSections>
 
</configSections>
 
<connectionStrings>
       
<add name="cnnLocal" connectionString="Data Source=.;Initial Catalog=DotNetFunda;Persist Security Info=True;User ID=sa;Password=sqluser" providerName="System.Data.SqlClient"/>
     
</connectionStrings>
 
<startup><supportedRuntime version="v2.0.50727"/></startup>
 
</configuration>

Conclusion


Hope this helps.
Regards,
Raj.Trivedi
"Sharing is Caring"
Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Posted by: Mymobyle on: 2/13/2013 | Points: 25
[http://www.mymobyle.com]good post[/http://www.mymobyle.com]
Posted by: Karthikreddy on: 2/14/2013 | Points: 25
Good article .keep write the articles

Login to post response

Comment using Facebook(Author doesn't get notification)