Link to home
Start Free TrialLog in
Avatar of udir
udir

asked on

ADO.NET and DataGridView....

Hi,
I wrote:

            SqlConnection con = new SqlConnection();
            con.ConnectionString = "data source=(local);initial catalog=Mashavit;integrated  
                                                                                                          security=sspi";
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tblCandidPhases", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridPhase.DataSource = dt;

when i update the fields in the DataGridView or when i add/delete rows, nothing happaned in the table - 'tblCandidPhases' (a table in sqlServer)
why?? what's wrong?
Thanks
(Sorry about the small points - it's all i'v got)
Avatar of DrAske
DrAske
Flag of Jordan image

You can use CurrentCellChanged event of the datagrid to dpdate the database .. this event is fired each time the value of CurrentCell property is changed in the datagrid ..

private void dataGridPhase_CurrentCellChanged(object sender, System.EventArgs e)
{
         OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
         da.Update(dt);
}

regards,Ahmad;
Avatar of udir
udir

ASKER

Hi Ahmad,
Thanks again for the reply :)
Did you ment -  'sqlCommandBuilder' ?
(why OleDbCommandBuilder  - i get an error massages).
and if i use the 'sqlCommandBuilder'  steel.... nothing happaned.
Thanks
OOPS!! it's Sql not OleDb .. it's a typo ;o)

take care ..
Avatar of udir

ASKER

Hi,
nothing happaned in the table, why?

the code is :

        private void dataGridPhase_CurrentCellChanged(object sender, System.EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "data source=(local);initial catalog=Mashavit;integrated security=sspi";
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tblCandidPhases", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            da.Update(dt);
        }
I stated that CurrentCellChanged event is for updating the database ..
not filling th dataset ..!!

private void dataGridPhase_CurrentCellChanged(object sender, System.EventArgs e)
{
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            da.Update(dt);
}
May be you'll get an Error Messages stated that *da* and *dt* object does not exist or something like that ..
so be sure to declare both of them in the class scope not in a method scope ..

regards,Ahmad;
Avatar of udir

ASKER

Ahmad, sorry for being annoying but there is somthing basic that i don't
understand:
If i write only the code you wrote:
private void dataGridPhase_CurrentCellChanged(object sender, System.EventArgs e)
{
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            da.Update(dt);
}

Where do i put the lines :

SqlConnection con = new SqlConnection();
            con.ConnectionString = "data source=(local);initial catalog=Mashavit;integrated security=sspi";
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from tblCandidPhases", con);
            DataTable dt = new DataTable();
            da.Fill(dt);

How 'da' and 'dt' will be recognized in the event if thay were not created in the Event? (in your code)

Again, sorry for that.....

ASKER CERTIFIED SOLUTION
Avatar of DrAske
DrAske
Flag of Jordan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll go offline now ;o)
I'll be back tommorrow morning ..

regards,Ahmad;
Avatar of udir

ASKER

OK
Thanks
Avatar of udir

ASKER

Ahmad, points are yours anyway, thanks  
i will be most greatfull if you could show me what to write
in the class scope and where  - do you meen to write a function or what?
(as you see i'm a bigginer in C# and in OOP so not everything is so clear to me).
Thanks
I'll give you example ..

public class form1 : System.Windows.Forms.Form
{
 // Whatever here

 SqlDataAdapter da; // it is declared in the class scope
 DataTable dt = new DataTable();

private void Form1_Load(object sender, System.EventArgs e)
{ // this is a method scope
  SqlConnection con = new SqlConnection();
            con.ConnectionString = "data source=(local);initial catalog=Mashavit;integrated security=sspi";
            con.Open();
           da = new SqlDataAdapter("select * from tblCandidPhases", con);
           da.Fill(dt);
// add codes to bind the datatable to the datagrid ..
}
 // another methods as neeeded ..
private void dataGridPhase_CurrentCellChanged(object sender, System.EventArgs e)
{ // because da and dt is declared in the class scope .. it will compile and work fine ..
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            da.Update(dt);
}
}

regards,Ahmad;
Avatar of udir

ASKER

Thanks Alot!!!!