-1

i'm new on .net, and i'm trying to make a gridview table that take data from a database (i bind data with <asp:sqldatasource selectcommand> tag) and for a specific integer value from this table column i want to display in gridview a string that is in another table and is specific for the integer. So 2 tables, 1 is inserted in gridview, another has static number of columns, table "a" has integers and other columns, tabel "b" has same integers but different strings on other columns for them. In gridview i want to show other columns from table "a" and 1 column from table "b". I can display the first table but i don't have ideas to link 2 tables. I can't make changes in databes. Thank you!

Table a                                                            Table b                      
column1 column2 column3                        column4 column5
data1    data2   integer                               integer  string

Output
Gridview
column1 column2 column5
Black
  • 3
  • 5
  • 2
    If you're new to .NET, why are you using ASP.NET WebForms? It's almost 20 years old and is very obsolete. You should not use it for new projects. – Dai Sep 30 '20 at 10:06
  • Yes, but it's a homework project that i have to do for learning some .net. – Black Sep 30 '20 at 10:13
  • 1
    I'm concerned that you'll be learning bad-habits and old, discredited, practices if you're learning with WebForms. There are many reasons why WebForms is obsolete. – Dai Sep 30 '20 at 10:14
  • It is not obsolete. "In fact, as part of the final version of the .NET Framework (4.8), ASP.NET Web Forms is considered an official Windows system component, which means its support life is tied to the life of Windows 10. This guarantees Microsoft support for Web Forms to at least 2025 — and probably far beyond that." [Here](https://blog.submain.com/case-web-forms-even-2018/) and [here](https://medium.com/young-coder/did-asp-net-web-forms-need-to-die-4b7bce958aaa), etc. – wazz Sep 30 '20 at 14:51
  • @wazz That’s a different definition of obsolete. Microsoft is not investing anything in WebForms beyond security updates, which means WebForms is permanently stuck in the year 2005. It doesn’t support modern HTML at all. – Dai Sep 30 '20 at 17:22
  • @wazz it's not just obsolete, it's dead - it will never be ported to .NET Core, which completely replaced .NET Old. There won't be any new .NET Old version. .NET 5 is actually .NET *Core* 5. Worse, WebForms used a quirky model suitable for desktop applications, not web applications and thus, can't be used to create modern web applications. So there really is no reason to learn WebForms, unless one intends to work on legacy projects. – Panagiotis Kanavos Sep 30 '20 at 18:11
  • 1
    @wazz btw you posted links to opinion blog posts. Microsoft itself [strongly advises people to move to Razor Pages instead, in WebForm's landing page](https://dotnet.microsoft.com/apps/aspnet/web-forms). The ASP.NET devs have been saying for a long time that [WebForms won't be ported](https://twitter.com/runfaster2000/status/1125459930041933824) as [there's no easy migration path](https://devblogs.microsoft.com/dotnet/net-core-is-the-future-of-net/#comment-1808) – Panagiotis Kanavos Sep 30 '20 at 18:18
  • Webforms can still be used. It is certainly legacy status, but has far less of a learning curve and is far easier to use. I still use them and still love them. It just fine and ok. Once one gets up to speed, then one can make the jump to MVC. But then again on the other hand? Well, when we go on SO and ask for software recommends? Everyone jumps in and says asking or recommending what software tools to use goes against SO policy. Is that not what you doing now? Go figure! – Albert D. Kallal Sep 30 '20 at 18:19
  • @PanagiotisKanavos I know that's what MS advises, but there's a **HUGE** difference b/t won't be ported and obsolete. Thousands of websites still rely on web forms and will for a long time. Just sick of people saying that like it doesn't exist. – wazz Sep 30 '20 at 21:10

1 Answers1

1

To display the other data, then simply use a left join in your sql.

So, say we have this to load up the data grid:

if (IsPostBack == false)
{
    GridView1.DataSource = Myrst("Select FirstName, LastName, Hotel_ID FROM tblBooked");
    GridView1.DataBind();
}

We thus get this result:

enter image description here

But, that hotel_id is rather ugly, so we want to pull that data from tblHotels

So, you simply left join in the other table. You can write out the sql, or say lets create a view like this:

enter image description here

Now, our simple code can say go like this:

    GridView1.DataSource = Myrst("SELECT * from vBookedHotels");
    GridView1.DataBind();

And we get this result:

enter image description here

So the "general" approach here is to write some sql and use a left join. You can thus quite much pull in any "id" value and translate it to the other table. So friendly text names or descriptions can thus be pulled from the other table.

I recommend using SQL for this, since then your two lines of code to load up the gridview can be done as per above. And it often possible that you need to do this in several places in your application - so a handy view to query against makes is rather easy.

in above, I use a custom routine called MyRst(), and all it does is create the sqlcommand object, get the connection and returns a data table (i was tired of writing the same code over and over (eg: create connection, create data adaptor etc - so I just put that code in a simple routine, and now I can just type in some sql and quite much assign it to a gridview, or even a listview, or even dropdown boxes with the two lines of code as per above.

So, the general approach here is to use SQL to get/grab/pull and translate some "ID" in a column to some nice user friendly description or text columns in the 2nd table as you outlined.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51