Answered executing SQL query in C#

  • Saturday, January 13, 2007 10:24 PM
     
     

    hi, suppose we have some text boxes already binded with BindingNavigator how to execute an SQL query using the code so the binded data can be specified with this SQL statement using C#.


All Replies

  • Saturday, January 13, 2007 10:36 PM
     
     

    Just say a textbox (txtMyID) is bind to a database filed called "MyID". So you can write UPDATE SQL like this

    string SQL = "UPDATE MyTable SET SomeField = 'Something' WHERE MyID = '"+ txtMyID.Text + "'"

    I don’t know is this the thing you are asking. But hope this will help you

  • Tuesday, January 16, 2007 6:44 AM
     
     

    thanx for your reply but this  is not what i want

    when you bind a textbox with the binding navigator control the data will apear on the text box and when you press the move next button, the binding navigator control will point to then next record, now the data that are being selected by the binding navigator is the whole database how can i execute an SQL query using the code to change the selection of the data and to make some filtration using the binding navigator control by code.

    regards,

  • Tuesday, January 16, 2007 7:27 AM
     
     

    Hi  Sculptor  I often use code as follow to add databinding to a bindingNavigator
    if I think we can cheng Datasource for the BindingSource to get any table you want to bind to the Navigator


    DBAccess DB = new DBAccess();
    DataTable TB= DB.getData().Tables[0];
    BindingSource bs = new BindingSource();
    bs.DataSource = TB.DefaultView;
    this.dataGridView1.DataSource = bs;
    this.bindingNavigator1.BindingSource = bs;

    I am not sure if it meet your question

    best Regards!

  • Thursday, January 18, 2007 11:00 AM
     
     

    thanx i think this piece of code would help but
    Lets suppose that we have this query "SELECT * FROM emp WHERE sal > 6000" and i want to execute this query using the code.

    regards

  • Thursday, January 18, 2007 11:45 AM
     
     Answered

    Try this

    string ConnectionString = "My Connection String";
    SqlConnection Conn = new SqlConnection(ConnectionString);
    Conn.Open();

    SqlDataAdapter DA = new SqlDataAdapter("SELECT * FROM emp WHERE sal > 6000", Conn);
    DataTable dt = new DataTable();
    DA.Fill(dt);

    if (Conn.State == ConnectionState.Open)
    {
        Conn.Close();
    }

    BindingSource BS = new BindingSource();
    BS.DataSource = dt;
    dataGridView1.DataSource = BS;
    bindingNavigator1.BindingSource = BS;

  • Friday, January 19, 2007 8:34 AM
     
     

    Thank you very much

    this is the exact answer that meets my desire;

  • Friday, January 19, 2007 8:55 AM
     
     

    Hi again

    Now what if I wanted to bind text boxes instead of data grid using the same piece of code. Lets suppose that we have textBox1 to be binded with (id) column at emp table and textBox2 to be binded with (name) at the same table, what would be the code for this purpose.

    Thanks

    regards,

  • Friday, January 19, 2007 8:17 PM
     
     Answered

    here is the code

    textBox1.DataBindings.Add("Text", BS, "id");
    textBox2.DataBindings.Add("Text", BS, "name");

  • Saturday, January 20, 2007 6:53 AM
     
     

    Thanks, this is exactly what i need to know

    regards,

  • Tuesday, June 19, 2012 5:36 AM
     
     

    Thanks, this is exactly what i need to know

    regards,