ASP.NET - hi

Asked By SYED ASGAR
09-Feb-12 02:13 AM
 can anybody help me in combaining two tables and display data in a gridview
thaNK YOU
  Reena Jain replied to SYED ASGAR
09-Feb-12 02:19 AM
hi,

Simple use join to get the data from both table in database query like this

select a.*, b.* from table1 as a inner join table2 as b on a.pk_id=b.fk_id

and assign the table data to gridview.

if(!isPostBack)
{
SqlConnection con=new SqlConnection();
con.ConnectionString="Your Connection String";
con.open();
Dataset ds;
SqlDataAdapter da;
da=new SqlDataAdapter("select a.*, b.* from table1 s1 inner join table2 s2 on s1.pk_id=fk_id",con);
da.fill(ds,"search");
Gridview1.DataSource=ds;
Gridview1.Bind();
}


Hope this will help you
  Danasegarane Arunachalam replied to SYED ASGAR
09-Feb-12 02:20 AM
Fetch the data using the below method

1.Create one data adapter
2.First fill the first table using Data adapter.Fill(table,1"Name for the table") Method
3.Then fill the second using  Data adapter.Fill(table2,"Name for the table") Method
4.Merge the two table using Datatable.Merge method
5.Show in gridview

public void BindGridview()

   {

     con=new SqlConnection(“Connectionstring”);

     da = new SqlDataAdapter(“Select * from Table1″, con);

     da.Fill(ds1, “Record”);

     da = new SqlDataAdapter(“select * from Table2″, con);

     da.Fill(ds2, “Record”);

     ds1.Merge(ds2);

     Gridview1.DataSource = ds1;

     
     Gridview1.DataBind();

   }

}


  Lalit M. replied to SYED ASGAR
09-Feb-12 02:26 AM
Hi..

Try below code sample

ASPX code sample
---------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
         <Columns>
           <asp:TemplateField>
             <ItemTemplate>
               <asp:LinkButton ID="lnkTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Title" %>' />
             </ItemTemplate>
            </asp:TemplateField>            
           <asp:TemplateField>
             <ItemTemplate>
               <asp:Label ID="lblDescription" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Description" %>' />
             </ItemTemplate>
            </asp:TemplateField>   
            <asp:TemplateField>
              <ItemTemplate>
               <asp:Label ID="lblCommentBody" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"CommentBody" %>' />
              </ItemTemplate>
            </asp:TemplateField>
          </Columns>
      </asp:GridView>                   
 

 CS Code sample
--------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Default_Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      SqlConnection sqlConnection1 = new SqlConnection(connectionString);

      SqlDataAdapter sqlAdapter1 = new SqlDataAdapter("SELECT TITLE, DESCRIPTON FROM TABLE1" , sqlConnection1);
      SqlDataAdapter sqlAdapter2 = new SqlDataAdapter("SELECT COMMENTBODY FROM TABLE2" , sqlConnection1);

      DataSet dataSet1 = new DataSet();
      DataSet dataSet2 = new DataSet();

      sqlAdapter1.Fill(dataSet1, "TABLE1");
      sqlAdapter2.Fill(dataSet2, "TABLE2");

      dataSet1.Merge(dataSet2, false, MissingSchemaAction.Add);
      GridView1.DataSource = dataSet1;
      GridView1.DataBind();
    }
}
  dipa ahuja replied to SYED ASGAR
09-Feb-12 02:54 AM
You can use  join query

select e.empid,e.sal,
e.Address,
ed.ename from edetail e inner join emp ed on e.empid=ed.empid;
 
Bind you table with this query Note : You must have a common column in both tables, where one must be primary key
  smr replied to SYED ASGAR
09-Feb-12 03:01 AM
hi

refer this

// SomeTable is the parent, OtherTable is the child
DataRelation dr = new DataRelation("MyRelation", ds.SomeTable.Columns["code"], ds.OtherTable.Columns["othercode"]);
 
// you can set the DataSource of the grid with either of the below syntaxes:
 
oGrid.DataSource = ds.SomeTable;
 
// -or-
 
oGrid.DataSource = ds.Relations["MyRelation"].ParentTable;


follow
http://aspdotnetcodebook.blogspot.com/2008/04/showing-data-from-multiple-tables.html
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/f63f612f-20be-4bad-a91c-474396941800
  Somesh Yadav replied to SYED ASGAR
09-Feb-12 03:16 AM
Hi here is the sample code .

/ method to fill the dataset
public void FillMyDataSet(MyDataSet ds)
{
    string sql = "SELECT a.*, b.* FROM MyTable a JOIN MyOtherTable b ON a.key = b.key";
    using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
    {
        da.Fill(ds, "MyTable");
    }
}


// call it like this
MyDataSet dsMine = new MyDataSet();
this.FillMyDataSet(dsMine);
MyGridView.DataSource = dsMine;
Create New Account
help
alot var Hi What are Master Pages in ASP.NET? or What is a Master Page? ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page. What are the 2 important parts of a master page? The following are the 2 important parts of a master page 1. The Master Page itself 2. One or more Content Pages Can Master Pages be nested? Yes, Master Pages
Page Directive? What is the purpose of page directive in aspx page? Directive Syntax Directives are instructions used to specify settings (related to how a page should render and processed) used by the page and user control compilers when they process ASP.NET Web Forms page (.aspx) and user control (.ascx) files. These are the essential part of every ASP.NET Page or Control. Directives can be located anywhere in an .aspx or .ascx file, but the values, same as any HTML tag) that are specific to that directive. Special Note: The @ Page directive can be used only in .aspx files, and the @ Control directive can be used
summary> / / / <param name = "command"> The SqlCommand to be prepared< / param> / / / <param name = "connection"> A valid SqlConnection, on which to execute this command< / param> / / / <param name = "transaction"> A valid SqlTransaction, or 'null was opened by the method, otherwose is false.< / param> private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection ) { if( command = = null ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders"); / / / < / remarks> / / / <param name = "connectionString"> A valid connection string for a SqlConnection< / param> / / / <param name = "commandType"> The CommandType (stored procedure, text, etc.)< / param> / / / <param name = "commandText"> The PublishOrders", new SqlParameter("@prodid", 24)); / / / < / remarks> / / / <param name = "connectionString"> A valid connection string for a SqlConnection< / param> / / / <param name = "commandType"> The CommandType (stored procedure, text, etc.)< / param> / / / <param name = "commandText"> The SqlParameter[] commandParameters) { if( connectionString = = null | | connectionString.Length = = 0 ) throw new ArgumentNullException( "connectionString" ); / / Create & open a SqlConnection, and dispose of it after we are done using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); / / Call the overload that takes a connection in place of the connection ExecuteNonQuery(connString, "PublishOrders", 24, 36); / / / < / remarks> / / / <param name = "connectionString"> A valid connection string for a SqlConnection< / param> / / / <param name = "spName"> The name of the stored prcedure< / param> / / / <param name = "parameterValues"> An
Remove(e.TabPage); } _cache.Add(e.TabPage); } } private void button1_Click(object sender, EventArgs e) { TabPage page = new TabPage("TabPage" + pageNumber.ToString()); AddControlsToTabPage(page); this.tabControl1.TabPages.Add(page); pageNumber++; this.tabControl1.SelectedTab = page; } private void AddControlsToTabPage(TabPage page) { page.Controls.Add(this.tab1CheckBox1); page.Controls.Add(this.tabCheckBox2 page.Controls.Add(this.tabCheckBox3); page.Controls.Add(this.tabCheckBox4); page.Controls.Add(this.tabCheckBox5); page