Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hallo i've tried many posibilities but i ain't getting it:
I have two comboboxes and a button. i wanna first choose from each combobox and dispaly the records in Form2 (datagridview)

C#
public partial class Form1 : Form
    {
       
private void button1_Click(object sender, EventArgs e)
        {
            //Connection string
            string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            SqlConnection con = new SqlConnection(C);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;

            //write a query
            Form2 f2 = new Form2();
            string qry = "";
            string qrynew = " select " + cbNr.SelectedItem  + " from " + cbTbl.SelectedItem + " where [DBN Zeichen] = 'AB' ";
            
            if (cbTbl.SelectedItem != null && cbEIU.SelectedItem != null)
            {
                    this.Hide();
                    f2.copyfun(qrynew);       
                    f2.show();
                }
                else
                {
                    MessageBox.Show ("ERROR First choose a tbl");
                }
            }
        }
    }



Form2:
C#
     public partial class Form2 : Form
    {
        public Standardbericht()
        {
            InitializeComponent();
        }

        DataTable dt;

        private void Standardbericht_Load(object sender, EventArgs e)
        {
            //lbTblname.Text = cbTbl.Text;
            string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            SqlConnection con = new SqlConnection(C);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;

        }
        // Get/ retrieve records from the previous page
        private void loadgrid(string qrynew)
        {
            string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            SqlConnection con = new SqlConnection(C);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = (qrynew);
            DataSet d = new DataSet();
            //dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(d);
            dgvSbericht.DataSource = d;
            //dgvSbericht.DataSource = d.Tables[0];

        }

        public void copyfun(string qrynew)
        {
            loadgrid(qrynew);
        }
    }
}



My Query
SQL
string qrynew = "SELECT " + cbNr.SelectedItem  + " from " + cbTbl.SelectedItem + " where [DBN Zeichen] = 'AB'";


And that's what i get if i debug it on line (cmd.CommandText = (qrynew):

Quote:
select 16 from System.Data.DataRowView where [DBN Zeichen] = 'AB'
Posted
Updated 19-Nov-14 8:54am
v3

cbTbl.SelectedItem is DataRowView, so you need to get the value as below
C#
cbTbl.SelectedItem.Row["ColumnName"].ToString()

and also cbNr.SelectedItem giving you value as 16 may be you want cbNr.SelectedItem.Text or cbNr.SelectedItem.Value depending on how you done the data binding.
any way this way of concatenating sql string is open for sql injection attacks. you better always try to use parameterized query
 
Share this answer
 
Comments
mikybrain1 19-Nov-14 14:08pm    
Thnx the problem is that i don't wanna choose the column name but rather from the FROM clause dynamically. Is it possible? Or how can i get the records in a different way?
DamithSL 19-Nov-14 14:11pm    
yes, some cases you have to build sql query but make sure that all the inputs are safe. You better include the codes of how you bind two combo boxes.
mikybrain1 19-Nov-14 14:23pm    
what do u please mean with "include the codes"? shd i post it for improvemennt?
DamithSL 19-Nov-14 14:26pm    
update the question with relevant code
mikybrain1 19-Nov-14 14:59pm    
I' ve updatet it. I 'trying to display certain columns of a dynamic table in a DB based on the selected items (also dynamic) to a second Form.
Would be glad if u can help
With a lot of intensity i finally solved it. Bravo


C#
public partial class Form1 : Form
    {
       
private void button1_Click(object sender, EventArgs e)
        {
            //Connection string
            string C = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            SqlConnection con = new SqlConnection(C);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = ("sp");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@table_name", cbTbl.SelectedValue);

 
            //write a query
            Form2 f2 = new Form2();
            string qry = "";
            string qrynew = cmd.CommandText
            
            if (cbTbl.SelectedItem != null && cbNr.SelectedItem != null)
            {
                    this.Hide();
                    f2.copyfun(qrynew);       
                    f2.show();
                }
                else
                {
                    MessageBox.Show ("ERROR First choose a tbl");
                }
            }
        }
    }
 
Share this answer
 

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