Click here to Skip to main content
15,917,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have coded like that but the datagridview is not showing the database. Kindly provide me good solution.

C#
public partial class frmMaterial : Form
    {
        public frmMaterial()
        {
            InitializeComponent();


            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";

            connection.Open();

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

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

            da.Fill(ds);
            cmbcode.DisplayMember = "Code";
            cmbcode.ValueMember = "Code";     

        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
           
            if (cmbcode.SelectedIndex >= 0)
            {
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";

                connection.Open();


                DataSet dl = new DataSet();
                string a = cmbcode.SelectedValue.ToString();
                

                SqlCommand com = new SqlCommand("select * from mat_utiAndtrans where Code='" + a + "'", connection);
               

                DataSet dts = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(com);
                dataGridView1.DataSource = dts;

                da.Fill(dts);

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

            }
Posted
Updated 19-Jan-14 17:07pm
v2

C#
public partial class frmMaterial : Form
    {
        public frmMaterial()
        {
            InitializeComponent();
 

            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";
 
            connection.Open();
 
            SqlCommand command = new SqlCommand("select * from mat_utiAndtrans", connection);
 
            DataTable ds = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            cmbcode.DataSource = ds;
 
            da.Fill(ds);
            cmbcode.DisplayMember = "Code";
            cmbcode.ValueMember = "Code";     
 
        }
 
        private void btnsearch_Click(object sender, EventArgs e)
        {
           
            if (cmbcode.SelectedIndex >= 0)
            {
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = "Data Source=ASA-PC; Initial Catalog=Material_database; User ID=sa;Password=sa;Integrated Security=false;Connect Timeout=60";
 
                connection.Open()
                string a = cmbcode.SelectedValue.ToString();
                
 
                SqlCommand com = new SqlCommand("select * from mat_utiAndtrans where Code='" + a + "'", connection);
               
 
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();              
                da.Fill(dts);
 
                dataGridView1.DataSource = dt;
 
            }
 
Share this answer
 
Change like this in frmMaterial method
C#
cmbcode.DisplayMember = "Code";
cmbcode.ValueMember = "Code";      
  da.Fill(ds);  // Fill the data first and then assign it to a control 
cmbcode.DataSource = ds;

in Search button click event

C#
da.Fill(dts);
dataGridView1.DataSource = dts;
 //dataGridView1.DataMember = dts.Tables[0].ToString();
 
Share this answer
 
Comments
Asa code 20-Jan-14 0:01am    
still the problem remains.... it's running without errors but not loading the form... :(
Karthik_Mahalingam 20-Jan-14 0:07am    
during on load or on search ??
the combobox is filling the data ??
Asa code 20-Jan-14 0:15am    
intention is should fill the datagridview only and combobox is to click and load the newform .... but issue is not showing anything now even the form is not loaded.... :(
Asa code 20-Jan-14 0:18am    
it's stopped and showed as

connection.Open();

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Karthik_Mahalingam 20-Jan-14 1:11am    
means your connection string is not valid..
pls check it...
check username, password. etc....
use SqlConnection string object connection in SqlDataAdapter object da and store query in string object as follows


C#
string com=sqlQuery;
SqlDataAdapter da= new SqlDataAdapter(com,connection);
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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