Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Binding data from SQL database to dataGridView control

gridview grid datagridview

  • Please log in to reply
11 replies to this topic

#1 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 18 August 2011 - 03:19 PM

i've searched a lot on google and didn't find my answer how to do that. I've create a server on SQL Server 2008 and named it Antonio-HP, I've created a database inside it and named it Proba1. How can I connect to my database with C# and fill datas from it to dataGridView control...don't bother with SQL query. I know what I have to do with it. Can someone help me?
  • 0

#2 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 18 August 2011 - 05:21 PM

I know their a more visual way with drag&drop from the form designer itself, but I don't remember how to do so.

So here the code way

String connectionString = "Data Source=localhost;Initial Catalog=database_Name;User ID=Username;Password=Password"
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = new SqlCommand("select * from tableName", connection);
command.CommandType = CommandType.Text;
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.CloseConnection);

Than you do a
while (reader.read())
And you have your data
  • 0

#3 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 19 August 2011 - 11:56 AM

i'm trying to connect to my own server but there is always an exception here is the code
string connectionString = "Data Source=ANTONIO-HP;Initial Catalog=Proba1;";
            try
            {
                SqlConnection connection = new SqlConnection(connectionString);
                connection.Open();
                MessageBox.Show("Veza je uspješno ostvarena");
                connection.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Nije ostvarena veza");
            }


  • 0

#4 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 19 August 2011 - 12:02 PM

Can you modify your code like this
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                MessageBox.Show("Nije ostvarena veza");
            }

And tell us what is the result of the first messagebox
  • 0

#5 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 19 August 2011 - 12:35 PM

ImageShack® - Online Photo and Video Hosting
this is the result of the first messagebox
  • 0

#6 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 19 August 2011 - 12:59 PM

You didn't put the user & the password of the database in your connectionString
;User ID=Username;Password=Password
Look at my first exemple
  • 0

#7 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 19 August 2011 - 02:21 PM

i have included it but exception said that it's wrong username...i put an unsername from SQL Server 2008 but it's still wrong
  • 0

#8 Vaielab

Vaielab

    Programming God

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1382 posts
  • Location:Quebec City
  • Programming Language:Java, C++, C#, PHP, JavaScript, Visual Basic .NET, Transact-SQL, ActionScript

Posted 19 August 2011 - 02:36 PM

Is it the same error message?
If so, have you tried using an IP insted of ANTONIO-HP
Is ANTONIO-HP your localhost or a other computer, if so try to desactivate every firewall
  • 0

#9 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 19 August 2011 - 02:48 PM

it's local host and the exception is

ImageShack® - Online Photo and Video Hosting
  • 0

#10 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 20 August 2011 - 05:19 AM

this is the exception when I'm using IP instead server name
ImageShack® - Online Photo and Video Hosting
  • 0

#11 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 20 August 2011 - 12:21 PM

I made it. I've changed connection string into
"Persist Security Info=False;Integrated Security=true;Initial Catalog=Proba1;server=(local)"
Now there is no exception during opening connection to my database
Now how to fill data from database into dataGridView control. Can someone give me an example for 2 columns
  • 0

#12 Tonchi

Tonchi

    Helping the world with programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1249 posts
  • Location:Zagreb
  • Programming Language:C#, Others
  • Learning:C, C++, Python, JavaScript, Transact-SQL, Assembly

Posted 21 August 2011 - 03:17 AM

i found some example on google but it seems it's not working properly
this is the code:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Database1
{
    
    
    public partial class Form1 : Form
    {
        SqlCommand sCommand;
        SqlDataAdapter sAdapter;
        SqlCommandBuilder sBuilder;
        DataSet sDs;
        DataTable sTable;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
	    }

        private void Form1_Load(object sender, EventArgs e)
        {
            string connectionString = "Persist Security Info=False;Integrated Security=true;Initial Catalog=Proba1;server=(local)";

           
            try
            {                
                SqlConnection conn = new SqlConnection(connectionString);

                conn.Open();
                MessageBox.Show("Veza je uspješno ostvarena");

                string sql = "SELECT first, last FROM employee";
                
                sCommand = new SqlCommand(sql, conn);
                sAdapter = new SqlDataAdapter(sCommand);
                sBuilder = new SqlCommandBuilder(sAdapter);
                sDs = new DataSet();
                sAdapter.Fill(sDs, "employee");
                sTable = sDs.Tables["employee"];
                
                conn.Close();
                dataGridView1.DataSource = sDs.Tables["employee"];
                dataGridView1.ReadOnly = true;
                
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                MessageBox.Show("Nije ostvarena veza");
            }
            


        }
	}
}
debugger don't give any error or warning but dataGridView doesn't show any data...why???
  • 0





Also tagged with one or more of these keywords: gridview, grid, datagridview

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download