-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTreeViewFromDb.cs
More file actions
64 lines (58 loc) · 1.93 KB
/
TreeViewFromDb.cs
File metadata and controls
64 lines (58 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
namespace TreeViewFromDb
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
treeView1.Nodes.Add("India");
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-C25MSTP\SQLEXPRESS; Initial Catalog=master; Integrated Security=true");
SqlCommand cmd = new SqlCommand("select distinct state from india", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
try
{
con.Open();
da.Fill(dt);
int i = 0;
foreach (DataRow drow in dt.Rows)
{
treeView1.Nodes[0].Nodes.Add(drow["state"].ToString());
SqlCommand cmd2 = new SqlCommand("select dist from india where state='" + drow["state"].ToString() + "'", con);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
foreach(DataRow drow2 in dt2.Rows)
{
treeView1.Nodes[0].Nodes[i].Nodes.Add(drow2["dist"].ToString());
}
da2.Dispose();
cmd2.Dispose();
i++;
}
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cmd.Dispose();
da.Dispose();
con.Close();
}
}
}
}