Hi people I need some help here.
Here is the CODE :


for Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace practice
{
    public partial class Form1 : Form
    {
        Form2 obj;
        public Form1()
        {
            InitializeComponent();
        }

        string str1 = "Data Source=Blackgold;Initial Catalog=MIS;Integrated Security=True";
        
        private void button1_Click(object sender, EventArgs e)
        {
            
            
            string s = comboBox1.SelectedIndex.ToString();
            obj = new Form2(s);
            obj.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(str1);
            con.Open();

            SqlCommand cmd = new SqlCommand("Select Name from company", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add(dr[0].ToString());
        }
        
            }
       
    }
}

FORM2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace practice
{
    public partial class Form2 : Form
    {
       string s1;
       public Form2(string s)
        {
            InitializeComponent();
            Convert.ToInt16(s1);
            Convert.ToInt16(s);
            s1 = s;
        }
       string str1 = "Data Source=Blackgold;Initial Catalog=MIS;Integrated Security=True";

       SqlDataAdapter da;
       DataSet ds = new DataSet();

        private void Form2_Load(object sender, EventArgs e)
        {
            SqlConnection con1 = new SqlConnection(str1);
            con1.Open();
            if (s1 == "0")
            {
                //string ccm = s1.ToString();                
                string str2 = "Select C_ID, BillNo from T1 where C_NAME = " + s1 + "";
                da = new SqlDataAdapter(str2,con1);
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            else if (s1 == "1")
            {
                //string cmc = s1.ToString();
                string str3 = "Select C_ID, BillNo from T1 where C_NAME = "+ s1 + "";
                da = new SqlDataAdapter(str3, con1);
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
        }
    }

In form1 i am taking the selected index from combobox and storing it in a string(by converting it). As you can see i want to use the index value of the combobox in form2 in order to display respective table. when i execute this it gives me error at this point da.Fill(ds) saying my "C_NAME" coloumn is a varchar and it cannot convert it into int. Can you suggest a solution for this.


Thanks and Regards
Ajinkya

First thing i don't know what you store in your table column "C_NAME" if you store number in this field then just change your form2 code of line number 35 to

string str2 = "Select C_ID, BillNo from T1 where C_NAME = '" + s1 + "'";

make the same change to line 43 too

if you store name in column "C_NAME" then you have to change code in form1 of line number 28

string s = comboBox1.Text;

I hope you understand.
if you steal get any problem feel free to ask your problem.........

Hey Praveen,

I did it but still grid view not showing records.
I know "selectedIndex" property of combo box is 'int' data type.
I want to use that property to display records from table 'T1'.
And also the problem is that I dont want to specify any "name" instead of "s1".

So, now as we know that index is an int how can we use it in form2 to show records??


Thanks.

If your table name is T1 then you have to store T1 in your string variable,
change your code at line 28 in form1

string s="T";
s =s+ comboBox1.SelectedIndex.ToString();

and you to use data member of datagrid
change line 38 and 46 to

dataGridView1.DataSource = ds;
 dataGridView1.DataMember = s1;

Best Of Luck............

Please Mark it solve if your problem is solve

hey thanks,

Problem solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.