Solved

ADO.NET and DataGridView....

Posted on 2006-10-20
13
397 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 debugging 2 35
This code tracks birthdays 3 60
Icons and Colors for Terms 3 24
c# string handling 1 27
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

12 Experts available now in Live!

Get 1:1 Help Now