A newbie needs some help. I have a custom SQL connection and what I wanna do is, I wanna create a DataSet using this SQL connection. And I wanna be able to call this dataset from any method within the form. Just to be clear let's say :
I have gridcontrol named gridcontrol1, a button called button1. When I click to the button it should bind that dataset to the gridcontrol.
So I want to be able to call this dataset with different events.
I don't know how to make that dataset be able to called from anywhere in the form. Where should I put my codes for my dataset and in what (in a method, a new class or somewhere else?)
Thanks in advance.
EDIT :
I knew it. It's so easy. But as I said I'm a newbie. So here is my solution :
I defined my connection, dataset etc. like :
public static SqlConnection conn;
public static DataSet ds;
....
So I solved my problem...
kiran kumarPosted Mar 2, 2010, 12:02 AM
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 populate
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=KIRAN\\SQLEXPRESS;Initial Catalog=kiran;Integrated Security=True");
DataSet ds;
SqlDataAdapter da;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
con.Open();
ds = new DataSet();
da = new SqlDataAdapter("select * from item", con);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
}
}
take a look at this code
MARK THE ANSWER IF IT HELPS YOU
Sam HobbsPosted Mar 1, 2010, 11:57 PM