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