C Sharp New Bie Question

Discussion in 'Programming' started by agentwazakashi, Jun 25, 2011.

  1. #1
    Why the data grid is not showing? Thanks


    Program.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace thisisatest
    {
     class DisconnectedDataform
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new DisconnectedDataForm2());
            }
        }
    }
    Code (markup):

    Form1.cs
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace thisisatest
    {
    
        partial class DisconnectedDataForm2 : Form
        {
            private SqlConnection conn;
            private SqlDataAdapter daCustomers;
    
            private DataSet dsCustomers;
            private DataGrid dgCustomers;
    
            private const string tableName = "employees";
    
            // initialize form with DataGrid and Button
            public void DisconnectedDataform()
            {
                // fill dataset
                Initdata();
    
    
                // set up datagrid
                dgCustomers = new DataGrid();
                dgCustomers.Location = new Point(5, 5);
                dgCustomers.Size = new Size(
                    this.ClientRectangle.Size.Width - 10,
                    this.ClientRectangle.Height - 50);
                dgCustomers.DataSource = dsCustomers;
                dgCustomers.DataMember = tableName;
    
                // create update button
                Button btnUpdate = new Button();
                btnUpdate.Text = "Update";
                btnUpdate.Location = new Point(
                    this.ClientRectangle.Width / 2 - btnUpdate.Width / 2,
                    this.ClientRectangle.Height - (btnUpdate.Height + 10));
                btnUpdate.Click += new EventHandler(btnUpdateClicked);
    
                // make sure controls appear on form
                
                Controls.AddRange(new Control[] { dgCustomers, btnUpdate });
            }
    
            // set up ADO.NET objects
            public void Initdata()
            {
                // instantiate the connection
                conn = new SqlConnection(
                   "Server=JEFFREY-PC\\SQLEXPRESS;DataBase=test;Integrated Security=SSPI");
                // 1. instantiate a new DataSet
                dsCustomers = new DataSet();
    
                // 2. init SqlDataAdapter with select command and connection
                daCustomers = new SqlDataAdapter(
                "select firstname, lastname from employees", conn);
    
                // 3. fill in insert, update, and delete commands
                SqlCommandBuilder cmdBldr = new SqlCommandBuilder(daCustomers);
    
                // 4. fill the dataset
                daCustomers.Fill(dsCustomers, tableName);
            }
    
            // Update button was clicked
            public void btnUpdateClicked(object sender, EventArgs e)
            {
                // write changes back to DataBase
                daCustomers.Update(dsCustomers, tableName);
            }
    
            private void DisconnectedDataForm2_Load(object sender, EventArgs e)
            {
              
    
            }
    
    
    
        }
    
    }
    Code (markup):

    form1.designer.cs
    
    
    namespace thisisatest
    {
        partial class DisconnectedDataForm2
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // DisconnectedDataForm2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Name = "DisconnectedDataForm2";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.DisconnectedDataForm2_Load);
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
    
    
        }
    }
    
    Code (markup):
     
    agentwazakashi, Jun 25, 2011 IP
  2. agentwazakashi

    agentwazakashi Member

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    I noticed that I don't have a constructor,So I rename DisconnectedDataform() to DisconnectedDataform2(),then I put the InitializeComponent, to the constructor and it runs. I am just wondering, why I got a warning message.

    warning:message

    The designer can't be show for this file, because none of the classes within it can be designed. The designer inspected the following classes in the file: DisconnectedDataform----The base class "Sysetm.Object can't be designed


    Also when I click the update button, I got this message:


    InvalidOperationException was unhandled
    Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information.


    I set up a primary key, so I don't know what is the problem?
     
    Last edited: Jun 25, 2011
    agentwazakashi, Jun 25, 2011 IP
  3. babureddy

    babureddy Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    y don't you just pass on the source files, if its not a problem. I can help you out.
     
    babureddy, Jun 28, 2011 IP
  4. agentwazakashi

    agentwazakashi Member

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Sorry being a newbie at .net I did not get this? How can I do that?
     
    agentwazakashi, Jun 28, 2011 IP
  5. babureddy

    babureddy Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What I said was, if you can send me the actual files, I can run them here, debug it and let you know the solution. that was the reason i said that if your code did not contain any confidential or Intellectual property, you can zip the files and send it to me.
     
    babureddy, Jun 28, 2011 IP
  6. agentwazakashi

    agentwazakashi Member

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    I see, the code is in the house,I will send it later, so I try a new one in my office.


    I tried, but I can't figure it out. Below is what I have on form1.cs. Suppose I add two buttons one is for insert and the other is for update. What I like is when I double click a row on the datagridview, and I change the content, the database should be updated when I click the update button. The same thing should happen on insert, when I type something below, and click the insert, it should be inserted. I tried but I can't figure it out.

    private void button1_Click(object sender, EventArgs e)
    {

    update code goes here

    }

    private void button2_Click(object sender, EventArgs e)
    {

    insert code goes here

    }


    In the above methods what should I code?


    Also what is the purpose of this? BindingSource Source = new BindingSource(), , it runs even without it.

    form1.cs
    
    public Form1()
            {
                InitializeComponent();
                string connect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=inventorydbase.mdb";
    
                OleDbConnection dbconnect = new OleDbConnection(connect);
                dbconnect.Open();
                OleDbDataAdapter dbadapter = new OleDbDataAdapter("select *from usertable",dbconnect);
    
                DataTable dt = new DataTable("usertable");
    
                BindingSource bSource = new BindingSource();
    
                bSource.DataSource = dt;
    
                dbadapter.Fill(dt);
    
                dataGridView1.DataSource = dt;
                
                
                
            }
    
    
    Code (markup):
     
    agentwazakashi, Jun 30, 2011 IP
  7. babureddy

    babureddy Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Instead of writing the code, you can simple bind the grid to a datasource control, and provide the insert and update commands. that should infact solve your problem.
     
    babureddy, Jun 30, 2011 IP
  8. babureddy

    babureddy Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Check out the code below, i am using a sqlserver, but it should be the same with access as in your case... this is in case you want to write code to bind, otherwise for your example you need not write any code at all...

    private void getData_Click(object sender, EventArgs e)
    {
    string customers = "SELECT * FROM Customers";
    using (SqlConnection con =
    new SqlConnection (ConfigurationManager.
    ConnectionStrings["northwind"].ConnectionString))
    {
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(customers, con);
    da.Fill(ds, "Customers");
    dataGridView.AutoGenerateColumns = true;
    dataGridView.DataSource = ds;
    dataGridView.DataMember = "Customers";
    }
    }

    In the above code, a dataset is bound to the gridview, hence the datamember specifies Customers to be taken. if you are using a datatable or dataview, you can directly specify it and no need of member.

    Hope you can solve the issue now.
     
    babureddy, Jun 30, 2011 IP
  9. agentwazakashi

    agentwazakashi Member

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    Hi thanks for the answer, I already been able to show the data on the grid, check my post above which is this.

    I tried, but I can't figure it out. Below is what I have on form1.cs. Suppose I add two buttons one is for insert and the other is for update. What I like is when I double click a row on the datagridview, and I change the content, the database should be updated when I click the update button. The same thing should happen on insert, when I type something below, and click the insert, it should be inserted. I tried but I can't figure it out.

    
    private void button1_Click(object sender, EventArgs e)
            {
        update code goes here
             
            }
    
    private void button2_Click(object sender, EventArgs e)
            {
        insert code goes here
             
            }
    
    
    Code (markup):
    In the above methods what should I code?


    Also what is the purpose of this? BindingSource Source = new BindingSource(), , it runs even without it.

    form1.cs
    
    public Form1()
            {
                InitializeComponent();
                string connect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=inventorydbase.mdb";
    
                OleDbConnection dbconnect = new OleDbConnection(connect);
                dbconnect.Open();
                OleDbDataAdapter dbadapter = new OleDbDataAdapter("select *from usertable",dbconnect);
    
                DataTable dt = new DataTable("usertable");
    
                BindingSource bSource = new BindingSource();
    
                bSource.DataSource = dt;
    
                dbadapter.Fill(dt);
    
                dataGridView1.DataSource = dt;
                
                
                
            }
    
    
    Code (markup):
     
    agentwazakashi, Jun 30, 2011 IP
  10. babureddy

    babureddy Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    dont understand why you are doing it. Since all you need is to bind the datatable to the gridview's datasource, you can simply remove it.
     
    babureddy, Jun 30, 2011 IP
  11. agentwazakashi

    agentwazakashi Member

    Messages:
    93
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #11
    Yeah you are right I am not just familiar with bindingsource. Like what I told earlier, what should I code in the boxes controls to update and insert a database and update the datagridviewcontrol, when I change the content and inserted on the grid?

    
    private void update_Click(object sender, EventArgs e)
            {
        what code should i put here?
             
            }
    
    private void insert_Click(object sender, EventArgs e)
            {
        what code should i put here?
           }
             
    
    public Form1()
            {
                InitializeComponent();
                string connect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=inventorydbase.mdb";
    
                OleDbConnection dbconnect = new OleDbConnection(connect);
                dbconnect.Open();
                OleDbDataAdapter dbadapter = new OleDbDataAdapter("select *from usertable",dbconnect);
    
                DataTable dt = new DataTable("usertable");
    
                BindingSource bSource = new BindingSource();
    
                bSource.DataSource = dt;
    
                dbadapter.Fill(dt);
    
                dataGridView1.DataSource = dt;
                
                }
                
    
    Code (markup):
     
    Last edited: Jun 30, 2011
    agentwazakashi, Jun 30, 2011 IP
  12. babureddy

    babureddy Peon

    Messages:
    197
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I am not supposed to post links here, I send you a link that contains details about BindingSource object. Check it out. once you send me your code, i will update it with the details on those events that you have asked for.
     
    babureddy, Jun 30, 2011 IP