A. Dubey A. Dubey - 2 months ago 9
Ajax Question

How to get a table on a new web page when a button is Clicked

This is my one tag:

<asp:Button ID="button" runat="server" Text="ShowOrder" onclick="newTab" />


This is my 'aspx.cs' which will be called when button is clicked

protected void newTab(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?id="+txtSearchCustomerByID.Value);
}


What I want is to print my sql table on loaded web tab (new page) when it gets loaded.

My stored procedure is displaying the data of my table where id is equal to "id entered by user in textbox".

Now,

protected void Page_Load(object sender, EventArgs e)
{
int id_no = int.Parse(Request.QueryString["id"]);
if (Page.IsPostBack)
{
showOrders(id_no);
}
}


Now what should I have in 'Default2.aspx' so that I will get my table by using,

public void showOrders(int id)
{
using (SqlConnection con = new SqlConnection(strConnString))
{
SqlCommand cmd = new SqlCommand("showOrdersSP", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
con.Open();
.......
}
}


I need to use DataTable

So simply when I clicked, I will get data table on new page

Answer

You can refer below code:

public void showOrders(int id)
   {
   using (SqlConnection con = new SqlConnection(strConnString))
   {
        DataTable dt = new DataTable();
        SqlParameter[] p1 = new SqlParameter[1];
        p1[0] = new SqlParameter("@id", id);    
        dt= getRecords_table("showOrdersSP", p1); // you will get your DataTable here

   }
   }

// Common Method for FillYour Tables
 private DataTable getRecords_table(string spname, SqlParameter[] para)
        {
            string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["connName"].ConnectionString.ToString();
            SqlConnection con = new SqlConnection(connectionstring);
            SqlDataAdapter ad = new SqlDataAdapter(spname, con);
            ad.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            ad.SelectCommand.Parameters.AddRange(para);
            con.Open();
            ad.Fill(dt);
            con.Close();
            return dt;

        }

Hope it will helps you

Thanks

Comments