Jump to content

Binding data from SQL database to dataGridView control

- - - - -

  • Please log in to reply
11 replies to this topic

#1
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
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?

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
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

#3
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
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");

            }




#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
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

#5
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
ImageShack® - Online Photo and Video Hosting
this is the result of the first messagebox

#6
Vaielab

Vaielab

    Programming God

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

#7
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
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

#8
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
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

#9
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
it's local host and the exception is

ImageShack® - Online Photo and Video Hosting

#10
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
this is the exception when I'm using IP instead server name
ImageShack® - Online Photo and Video Hosting

#11
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
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

#12
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 471 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
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???




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users