Solved

ADO.NET and DataGridView....

Posted on 2006-10-20
13
396 Views
Last Modified: 2012-05-05
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
Comment
Question by:udir
  • 7
  • 6
13 Comments
 
LVL 9

Expert Comment

by:DrAske
ID: 17775907
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
 
LVL 1

Author Comment

by:udir
ID: 17776150
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
 
LVL 9

Expert Comment

by:DrAske
ID: 17776176
OOPS!! it's Sql not OleDb .. it's a typo ;o)

take care ..
0
 
LVL 1

Author Comment

by:udir
ID: 17776427
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
 
LVL 9

Expert Comment

by:DrAske
ID: 17776993
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
 
LVL 9

Expert Comment

by:DrAske
ID: 17777038
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:udir
ID: 17777101
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
 
LVL 9

Accepted Solution

by:
DrAske earned 50 total points
ID: 17777154
>>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
 
LVL 9

Expert Comment

by:DrAske
ID: 17777264
I'll go offline now ;o)
I'll be back tommorrow morning ..

regards,Ahmad;
0
 
LVL 1

Author Comment

by:udir
ID: 17777357
OK
Thanks
0
 
LVL 1

Author Comment

by:udir
ID: 17777592
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
 
LVL 9

Expert Comment

by:DrAske
ID: 17779244
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
 
LVL 1

Author Comment

by:udir
ID: 17779764
Thanks Alot!!!!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now