How
to bind all tables data with Message Box Control in Windows Application
Note:
In this topic we will discuss with Dataset, Data Adapter
and how to bind all tables data in Message Box of Windows Application…………………………
Execute this below query in Sql Server……………………………………………
--create database test
use test
--create table p_Customer in database test
create table p_customer ( id int primary key, fname nvarchar(50), lname nvarchar(50), dobnvarchar(50))
--create table MyFriendList in database test
create table
MyFriendList(SrNo int identity(1,1)
primary key,Name
nvarchar(50),Emailid nvarchar(50)
unique ,MobileNo nvarchar(50)unique,Address nvarchar(50),Course nvarchar(50))
this source code for From1.cs……………………………………………
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using
System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using
System.Threading.Tasks;
using
System.Windows.Forms;
namespace Customer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlDataAdapter da;
DataSet ds;
private void btnFill_Click(object sender, EventArgs e)
{
string ConStr = "Data
Source=KUSH-PC;Initial Catalog=test;User ID=sa;Password=tiwari";
string cmd = "select *
from p_customer;select * from MyFriendList";
da = new SqlDataAdapter(cmd, ConStr);
ds = new DataSet();
da.Fill(ds);
MessageBox.Show("Dataset is
filled");
}
private void btnShow_Click(object sender, EventArgs e)
{
dataGridView1.DataSource =
ds.Tables[0];
}
// code for how to find each cell of Table in MessageBox one by
one
private void
btnShowMessageBox_Click(object sender, EventArgs e)
{
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i <
dt.Columns.Count; i++)
{
MessageBox.Show(dt.Columns[i].ColumnName
+ "-> " +
dr[i].ToString());
}
}
}
}
// code for how to find each
row of Table in Message Box one by one
string str="";
private void
btnMessageBoxPart2_Click(object sender, EventArgs e)
{
foreach (DataTable dt in ds.Tables)
{
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i <
dt.Columns.Count; i++)
{
str +=
dt.Columns[i].ColumnName + " -> " + dr[i].ToString()+" ";
}
MessageBox.Show(str);
str = "";
}
}
}
// code for how to bind all table data in MessageBox one by one
private void
btnShowAllTable_Click(object sender, EventArgs e)
{
foreach (DataTable dt in ds.Tables)
{
str += "Details for
table:"
+ dt.TableName + "\n";
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i <
dt.Columns.Count; i++)
{
str +=
dt.Columns[i].ColumnName + "->" + dr[i].ToString() + "\t";
}
str += "\n";
//MessageBox.Show(str);
//txtresult.Text = str;
}
}
MessageBox.Show(str);
}
}
}
Result:
When you select ds with
break point you find
this picture given below and show how
many tables comes with dataset
0 comments:
Post a Comment