You must Sign In to post a response.
  • Category: ASP.NET

    How to generate grid wrting a quary in textarea

    Hello Sir/Madam

    I want to create a one screen which contains one textarea and one button
    When i write select query on text area all the records are filled in one grid view
    I want only access Select Query , I have used My sql (database)

    Could you please suggest on this How can i perform this

    No of column is not fixed it will be displayed based on the select query


    Thanks,
    Bhavik Shah
  • #767259
    Hi,
    Please put runat="server" attribute for textarea, so that it will be accessible at code behind.

    <textarea id="txtArea1" runat="server"> Enter text here... </textarea>

    As your number of columns are not fixed, so make gridview property AutoGenerateColumns="true".

    <asp:GridView runat="server" ID="gvDemo" AutoGenerateColumns="true">
    <asp:GridView>

    Now in select sql script pass your textarea value in where condition as follows:

    SqlConnection con = new SqlConnection("ConnectionString");
    con.Open();
    SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM TableName Where ColumnName= "+ txtArea1.Value, con);
    DataTable dt = new DataTable();
    a.Fill(dt);
    con.Close();
    gvDemo.DataSource= dt;
    gvDemo.DataBind();

  • #767292
    Hi,

    If you want to execute select query based on user entry then you can pass the query commander through the textarea and get the data from database and then bind the same to your gridview and make sure the gridview AutogenerateColumns property should be true, because as you said you don't have fixed columns you want to make it as based on query so kept the gridview autogeneratecolumns should be true.


    Protected void btnClick(object sender, EventArgs e)
    {
    cmd = new SqlCommand(txtarea.Text, con);
    dt = new DataTable();

    try
    {
    con.Open();
    da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
    GV.DataSource = dt;
    GV.DataBind();
    }

    }
    catch (Exception ex)
    {

    }
    finally
    {
    con.Close();
    con.Dispose();
    }
    }


    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments