Click here to Skip to main content
15,889,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi

this is my c# code in form

C#
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = "Data Source=ASA-PC\\SQLEXPRESS; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=10";

            connection.Open();

            SqlCommand command = new SqlCommand("select * from Code", connection);

            DataTable ds = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            cmbcode1.DataSource = ds;

            da.Fill(ds);
            cmbcode1.DisplayMember = "Codes";
            cmbcode1.ValueMember = "Codes";   

        }


in search button code is

C#
if (cmbcode1.SelectedIndex >= 0)
            {
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = "Data Source=ASA-PC\\SQLEXPRESS; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=10";
                connection.Open();
                DataSet dl = new DataSet();
                string a = cmbcode1.SelectedValue.ToString();

                SqlCommand com = new SqlCommand("select * from Code where Codes='" + a + "'", connection);
                DataSet dts = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(com);
                dataGridView2.DataSource = dts;

                da.Fill(dts);
                dataGridView2.DataMember = dts.Tables[0].ToString();
            }



I need to get full data from database to datagridview .... but it just shows only one data
can you kindly help me to solve this problem?
Posted
Updated 23-Jan-14 17:06pm
v3

DataSet
C#
SqlCommand com = new SqlCommand("select * from Code where Codes='" + a + "'", connection);
DataSet dts = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(com);
da.Fill(dts);
dataGridView2.DataSource = dts.Tables[0];


DataTable
C#
SqlCommand com = new SqlCommand("select * from Code where Codes='" + a + "'", connection);
SqlDataAdapter da = new SqlDataAdapter(com);
DataTable dt = new DataTable
da.Fill(dt);
dataGridView2.DataSource = dt;


You can either of the options to fill your gridview.!

Good Luck
 
Share this answer
 
Comments
Asa code 24-Jan-14 1:21am    
still it showed only the first row of datagridview not showing the full data grid view
DataSet dts = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(com);
                dataGridView2.DataSource = dts;


Where do you fill the data set using your connection ?

dataGridView2.DataMember = dts.Tables[0].ToString();


What ever you think this does, you are wildly mistaken.

I suggest that you both read up on SQL injection attacks, to fix your code at the core, and on how to use C# to access a database, because you clearly don't know. Overall, I'd suggest using entity framework instead, because it's perhaps more intuitive than using a straight data layer, and you're clearly lost right now.

Here[^] is an article on assigning data to a datagridview. After you work through it, if you're still stuck, by all means, ask more questions and we'd love to help.
 
Share this answer
 
v2
Comments
Ron Beyer 23-Jan-14 22:55pm    
"What ever you think this does, you are wildly mistaken." Ha! Love it. +5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900