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"