473,569 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2814
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 SqlCommandBuild er
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.co m

<Je*****@gmail. com> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.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.co m

<Je*****@gmail. com> wrote in message
news:11******** **************@ h76g2000cwa.goo glegroups.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.Creat eInstance() 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.Componen tModel.Componen t) 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.Creat eInstance() to an
adapter object?
May 31 '06 #6
Nope, the adapter objects that are generated by VS inherit from
System.Componen tModel.Componen t. 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().get Method("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.co m

<Je*****@gmail. com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Ok, I figured out what I needed. I found I could use the
x.getType().get Method("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
2829
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# using ado.net. Please help me. Have good day. Thanks for your help in advance.
0
1693
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 the record, a preallocated static buffer size will have to be declared beforehand in the declaration section. Since the record sizes vary a lot,...
1
13642
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 procedure============= Create PROCEDURE get_timedout_scripts (
1
2269
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 updated but what happen is: form is postedback but no change happen to database and also textboxes return to there original state i.e previous...
22
2674
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 things. So, in instances where I list, for example, the latest weblogs. I list the headline of the weblog, the date and the name of the member who...
5
2970
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 TO TABLE, like storing IMAGE into DB.
1
1816
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 error. But now problem in fetching, not able to fetch data from table correctly. Here is my code. I hav following problems with this coding
0
1283
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 from the url string Imagename = DropDownList1.SelectedItem .Text ; //string Imagename = Request.QueryString; //build our query...
2
2665
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 table inserted but some not. But I get this error: The SqlCommand is currently busy Open, Fetching. Description: An unhandled exception occurred...
4
4260
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...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...

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.