2 Replies - 4840 Views - Last Post: 12 May 2012 - 02:47 PM Rate Topic: -----

#1 sirko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-May 10

C#, how to edit database using dataGriedView component?

Posted 12 May 2012 - 02:13 PM

I am writing a win forms app that would access a postgres database, display the table(s) content and allow user to edit it(Insert, Delete, Edit Rows).I figured how to display it in dataGridView, but I ran our of ideas how to Delete and Edit Rows.

Here are my attempts:

private void button1_Click(object sender, EventArgs e)
    DataSet ds = new DataSet();
    NpgsqlConnection conn = new NpgsqlConnection("Server=;Port=5433;Database=zavdDB;User Id=postgres;Password=1111");
    NpgsqlDataAdapter da = new NpgsqlDataAdapter("Select * from bandname", conn);
    da.InsertCommand = new NpgsqlCommand ("INSERT INTO table1 VALUES(@param1, @param2)", conn);
            DataSet ds = new DataSet();

But I realized I didn't know how to make it work so I started all over again

public Form1()
           ds = new DataSet();
            // 1. Create connection.
            conn = new NpgsqlConnection(@"Server=;Port=5433;Database=zavdDB;User Id=postgres;Password=1111");
            // 2. init SqlDataAdapter with select command and connection
            NpgsqlDataAdapter da = new NpgsqlDataAdapter("Select * bandname", conn);
            // 3. fill in insert, update, and delete commands
            NpgsqlCommandBuilder cmdBldr = new NpgsqlCommandBuilder(da);
            da.Fill(ds, "bandname");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "bandname";
         private void button3_Click(object sender, EventArgs e)
            da.Update(ds, "bandname");

What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: C#, how to edit database using dataGriedView component?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C#, how to edit database using dataGriedView component?

Posted 12 May 2012 - 02:18 PM

Q:... how to do x,y,z with a database {probably for the first time}...
A: Read this tutorial
Entire section of tutorials
Parameterizing Your SQL Queries: The RIGHT Way To Query A Database.
Using SqlDependency to monitor SQL database changes

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readible.

  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot


    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

This post has been edited by tlhIn`toq: 12 May 2012 - 02:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: C#, how to edit database using dataGriedView component?

Posted 12 May 2012 - 02:47 PM

Just a FYI, but every single book I've read on C# programming (ones not about specific areas, such as WCF) (and the number is 20+, I review books :)) covers exactly how to do this. It would not hurt you to find a book and see what you can learn. I'm always surprised by stuff I find :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1