Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

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)
0
udir
Asked:
udir
  • 7
  • 6
1 Solution
 
DrAskeCommented:
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;
0
 
udirAuthor Commented:
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
0
 
DrAskeCommented:
OOPS!! it's Sql not OleDb .. it's a typo ;o)

take care ..
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
udirAuthor Commented:
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);
        }
0
 
DrAskeCommented:
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);
}
0
 
DrAskeCommented:
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;
0
 
udirAuthor Commented:
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.....

0
 
DrAskeCommented:
>>Where do i put the lines :
If you want the datagrid to show the data as your application is started Then put it in *Load* event of your form ..
or - If you are Filling the datagrid by pressing a button - then put it in *Click* event of the button ..

>>How 'da' and 'dt' will be recognized in the event if thay were not created in the Event? (in your code)
declare it in the class scope ..

0
 
DrAskeCommented:
I'll go offline now ;o)
I'll be back tommorrow morning ..

regards,Ahmad;
0
 
udirAuthor Commented:
OK
Thanks
0
 
udirAuthor Commented:
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
0
 
DrAskeCommented:
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;
0
 
udirAuthor Commented:
Thanks Alot!!!!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now