Solved

ADO.NET and DataGridView....

Posted on 2006-10-20
13
399 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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