Database Question

Ok. I have a database in attached to my program. What I would like the query to do is this.

SELECT textBox1.Text FROM TableName;

Here is the code I have now.

private void doQuery_Click(object sender, EventArgs e)

{

sqlDataAdapter1.SelectCommand.CommandText =

"SELECT" + textBox1.Text + "FROM TableName";

dataSet1.Clear();

sqlDataAdapter1.Fill( dataSet1, "TableName" );

dataGrid1.SetDataBinding( dataSet1, "TableName");


But this isnt working. Can someone please tell me how to complete this code. It would be much appreciated.




Answer this question

Database Question

  • shaper

    Craig,

    Try this mod:



    private void doQuery_Click(object sender, EventArgs e)

    {

    sqlDataAdapter1.SelectCommand.CommandText =

    "SELECT" + textBox1.Text + "FROM TableName";

    dataSet1.Clear();

    sqlDataAdapter1.Fill( dataSet1, "TableName" );

    //dataGrid1.SetDataBinding( dataSet1, "TableName");
    dataGrid1.DataSource = dataSet1.Tables["TableName"];
    dataGrid1.DataBind();


     



    I am assuming this code is used in an ASP.NET Web Form.  If it is in a Windows From you do not need the 'dataGrid1.DataBind();' call because it is automatic.

    -David Sandor



  • smcandrew

    Ok. That did work, but there is still a problem.


    Here is my code:

    private void button1_Click(object sender, EventArgs e)

    {

    sqlDataAdapter1.SelectCommand.CommandText =

    "SELECT" + textBox1.Text + "FROM TabelName";

    dataSet1.Clear();

    sqlDataAdapter1.Fill( dataSet1, "TabelName" );

    dataGrid1.SetDataBinding( dataSet1, "TableName").

    When I input * into the textbox, the SQL Server reads:

    SELECT * FROM TabelName

    And that works, and displays the tabel, but when I input a certain column it doesnt work. When I put "AuthorID", (SELECT AuthorID FROM TabelName) it doesnt work, and doesnt display the tabel.

    Can you tell me any suggestions.



  • Bill Bickford

    Nope. I still get the exception error "Could not find stored procedure "SELECTAuthorIDFROM"

  • RobertLB

    Not knowing the exact error you are getting my first guess is that the SQL is not properly formatted.  Try:

    "SELECT " + textBox1.Text + " FROM TableName";

    Note the spaces after the SELECT and before the FROM.  From your code example you would end up with something like this:

    SELECTCOLUMNAMEFROM TableName which is not valid SQL.


  • Amit.Nayar

    Which is exactly what I said you would get in my first post.

    Your SQL string needs to be

    "SELECT " not
    "SELECT"

    Again notice the space after the "T" and before the ' " '.

    And also
    " FROM" not
    "FROM"

    Your SQL statement is invalid.  Your database doesn't understand the command "SELECTAuthorIDFROM" but it will understand "SELECT AuthorID FROM".



  • duda65607

    Posted too late ;)  What is the error you get

    -David Sandor

  • Brendon123

    Ok, I got it to work. And no, it was in a windows application. Big Smile

    And I just thought I would add another way to do it as well, with you of course as my inspiration.

    private void button1_Click(object sender, EventArgs e)

    {

    sqlDataAdapter1.SelectCommand.CommandText =

    "SELECT" + " " + textBox1.Text + " " + "FROM TabelName";

    dataSet1.Clear();

    sqlDataAdapter1.Fill(dataSet1, "TabelName");

    dataGrid1.SetDataBinding(dataSet1, "TabelName");

    }

    Sorry for not understand your first answer.


  • Database Question