473,218 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

Fetching data from SQL Server

I'm new to C# (and Microsoft in general) so this may be a simple
problem, but I haven't been able to figure it out yet.

I've got a database with a number of different tables and I want the
user to be able to select which table they want, then have that table
pop up in a datagridview. I've figured out how to get it to work if I
specify the table adapter to use and do it all manually, but I'd like
to make a method that will populate the datagridview with data when it
is given the appropriate table name. As it is now the only way I can
get it to work it is having a giant switch statement. I'd like to go
for a much more elegant solution.

I'm using VS 2005 and right now all the database interfaces are
whatever VS auto-generated when adding a new binding source. All the
examples I've seen so far either involve specifying which data adapter
to use at compile time (giving me the big switch statement) or running
all my own SQL queries (which isn't really an ideal solution either).

May 31 '06 #1
9 2793
Why not just create a SqlCommand with the statement:

"select * from " + table

Then, pass that to a SqlDataAdapter and fill a dataset with it. Once
you have that, you can just bind the grid to the data set and have the grid
auto-generate the columns for you.

Of course, if you are going to update the fields, then that's going to
require more work. Generally though, you could use the SqlCommandBuilder
class to generate the Insert, Update and Delete commands to attach to the
data adapter, passing your dataset back to the adapter for updating.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Je*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
I'm new to C# (and Microsoft in general) so this may be a simple
problem, but I haven't been able to figure it out yet.

I've got a database with a number of different tables and I want the
user to be able to select which table they want, then have that table
pop up in a datagridview. I've figured out how to get it to work if I
specify the table adapter to use and do it all manually, but I'd like
to make a method that will populate the datagridview with data when it
is given the appropriate table name. As it is now the only way I can
get it to work it is having a giant switch statement. I'd like to go
for a much more elegant solution.

I'm using VS 2005 and right now all the database interfaces are
whatever VS auto-generated when adding a new binding source. All the
examples I've seen so far either involve specifying which data adapter
to use at compile time (giving me the big switch statement) or running
all my own SQL queries (which isn't really an ideal solution either).

May 31 '06 #2
Well, for writing back the user will have an option. They can either
write back to the database or they can write out to an XML file. I've
gotta go through and look at whether I'll run into synchronization
problems if the database changes after loading data into the
datagridview... The user also has the option to filter/sort some of
the data, not sure if thats going to change which way the
implementation should go though.

May 31 '06 #3
Yes, you will have to handle synchronization on your own. The dataset
is disconnected (that's the model in .NET), and you have to account for when
changes occur to the database when saving back.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Je*****@gmail.com> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com...
Well, for writing back the user will have an option. They can either
write back to the database or they can write out to an XML file. I've
gotta go through and look at whether I'll run into synchronization
problems if the database changes after loading data into the
datagridview... The user also has the option to filter/sort some of
the data, not sure if thats going to change which way the
implementation should go though.

May 31 '06 #4
In an effort to make this as generic as possible (I've got multiple
database versions that this may be connecting to) I'm looking at
creating the appropriate tableadapter using the
Activator.CreateInstance() method. I'm creating the object ok, but now
I can't figure out how to get to its fill method. There's no common
interface or parent with the method (they all derive from
System.ComponentModel.Component) so I can't cast it to something
generic, and I can't figure out how to create my own interface since
the fill method (the main one I need right now) takes a table-specific
dataTable...

May 31 '06 #5
>>I can't cast it to something generic...

You can't just cast this object returned by Activator.CreateInstance() to an
adapter object?
May 31 '06 #6
Nope, the adapter objects that are generated by VS inherit from
System.ComponentModel.Component. I ended up going through and putting
together a special interface that had a fill method that took a generic
DataTable (since the fill methods generated took specific tables), then
I had to implement this second fill method for all of the tables. Its
not as clean as I would have liked, but it seems to be working (for
now). All the added code was identicle except for the table names, so
copy/paste with some regular expression replacements made it go pretty
quick. If there's a way to force the compiler to wait till runtime to
check the object type on the adapter that would make things a lot
easier...

May 31 '06 #7
Ok, I figured out what I needed. I found I could use the
x.getType().getMethod("Fill").Invoke(x, table) to get it to work.

May 31 '06 #8
I don't understand why you need this. After all, you can create any old
data adapter and assign the appropriate commands to the properties. You
don't need a typed one.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Je*****@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
Ok, I figured out what I needed. I found I could use the
x.getType().getMethod("Fill").Invoke(x, table) to get it to work.

Jun 1 '06 #9
I may not, but if I understand how things work correctly to do it using
normal untyped adapters I'd have to write the appropriate SQL
statements every time I created the adapter. That would be all well
and fine if I only had a handful of instances where I need an adapter,
but I'm looking at something like 400 or 500 possible different
instances. Its much easier to let VS auto-generate the majority of
those adapters so that all I have to write are the ones that don't
correspond to a single complete database table.

Jun 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Srinivasan R | last post by:
Hi, I am trying to get asynchronous fetching of data from sql server database. I would like to show the progress of fetching records in GUI. Is there any simple/complex steps for the same in C#...
0
by: Shujun Huang | last post by:
Hi, I am working on converting Informix database to Postgre. I have one question for fetching records using PostgreSQL. The record I am fetching is a variable size text string. Before fetching...
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
1
by: nasirmajor | last post by:
Dear All, im fetching record from database using datareader, and is displaying them in the textboxes. when user change the text fields and then presses the update button the record should be...
22
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other...
5
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE...
1
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any...
0
by: punitshrivastava | last post by:
Hi to All Senior's, I am working in Asp.net with C#. I am newbie in C#. So i want to fetch mutiple images from database .Database i am using is Sql Server. For this i code: //get the image id...
2
by: SunshineInTheRain | last post by:
I'm trying to modify a long long code within a button click by make the insert/update/delete/select using the same transaction. Purpose is to make sure every operation can be rollback instead of some...
4
by: tokcy | last post by:
HI every one, i am using tooltip on click of link and i want like when that tooltip open then background window would be blure(). can anyone help me...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.