usercontrol to connect to a SqlServer database - c#

I've been writing some small maintenance/viewer tools that each connect to a SQLServer (2005) database via System.Data.SqlClient classes.
(C# - .NET 3.5 - Windows.Forms)
For each of these tools I had to write the UI and dialog to let the user choose the right db and server: I connect to my test-db, my colleague uses my tools too and connects his test-db.
I'm looking for a plugable usercontrol that lets the user select a server, db and the rest of the connection parameters and that provides a SqlClient.SqlConnection to the rest of the application.
I've tried writing a usercontrol myself now, but I'm sure there must be others ,better ones and better tested ones. I've been Googling and looking at CodeProject, but found none.

There must be some better than one I wrote recently, because that one's so bad:
Just
Create a user control
drag a couple of buttons to it, anchor bottom center, make them ok and cancel
drag a Property Grid control onto it, dock fill
Add a read-only property to the control of type SqlConnectionStringBuilder, back it with a field
Initialize the field to a new SqlConnectionStringBuilder instance
In the Load event, set the Object property of the property grid to the SqlConnectionStringBuilder
That's pretty much that. The user just gets to fill in the properties. If you like, you can also create a ConnectionString property on the control and have it return the .ConnectionString property of the SqlConnectionStringBuilder.

What you're describing here is simple enough that I doubt there's something out there that does only this. You'd probably be better off sticking with your own code here.

Related

Show and hide switch in C# database applications

I have a small one line application and recently I have been instruct to remove all persons form it. That is I have several fields and one of it is a owner field. I am not going to move the owners from the database but only hide them and I need to be able to show some fields again when I will be contacted by that person saying: "You can show my name". This is an Visual Studio application with underlying SQL database. My dream solution would be a switch that make this name field true or false so I will be able to show only those name which I am allowed to show with a click of a button.
What do you think would be the easiest way to accomplish this.
There are lots of ways to do this, since you didn't specify what type of C# application you have (ASP.NET C#, WinForms, WCF, WPF), I'll assume you mean either Winforms or WPF. The easiest way is a checkbox, and then on change, hide or show the other fields that you want to hide. Then do your database calls. That's all I can give you without more info.
There are many ways to achieve that. One way is to introduce an extra bit field/column in your ‘User’ (or whatever it is) database table that would represent whether to show or hide a user name. Based on this field you would show or hide the user name within your application. This could be checked/unchecked directly in your database (by you), or via an application UI. You could implement ‘Admin’ page/view/form that would be dedicated to admins (you) that would have enough privileges to update the above specified column.

Save Settings in VB.Net or C#

How I can save the settings I want even after I close the program?
Let's say: If I have a Checkbox and I run the program. If I checked it I want to remain that way even when I reopen again the program. I hope you understand what I want.
I'm a newbie, so take me slow. Thank you.
I recommend using Application Settings Property Binding.
There are many options as mrunion mentioned in his good answer, but I think the most simple way in Windows Forms Application is using Application Settings Property Binding. To do so using designer:
Select your CheckBox in design surface
In properties window, at top, expand (ApplicationSettings), open dropdown for Checked property and select (New...) at bottom of dropdown.
Add your desired property with default value.
Save settings in somewhere like Form_Closing event:
C#: Properties.Settings.Default.Save();
VB: My.Settings.Save()
(ApplicationSettings) in property window:
Expanded from above....
To persist settings between different program runs, you will have to store that data somewhere for the user. Assuming since you mentioned VB, I will also assume Windows as your target platform. The best options are the following:
Use the registry to store the persistent data. There are plenty of tutorials on setting and retrieving registry variables.
Use a configuration/INI file for your application. Again, plenty of tutorials exist for this option.
Use a database. This is more advanced, but allows for the most flexibility of storing and retrieving data. I would suggest not using this method at first, and revisit it when you are ready to learn database design/querying/etc.
This should point you in the right direction.

Advanced GUI and database in c#

Do you have any idea how to present all rows from let's say table with the possibility to click on particular row and open that way another window to edit?
I've got no idea how to create this. I would like to avoid access like creation by built-in wizards in Microsoft Visual Studio 2008.
Perhaps you know where I can find more information.
Execute a query which retrieves an overview of the records that you want to display.
When you double-click a row, you retrieve the records that represent that entity, and display it in another window...
That's in a nutshell how you could do it.
For a web application you may want to look at this Walkthrough as MSDN. You can find a winform walkthrough at MSDN as well. Though you say that you prefer doing it without the designers, I suggest that you go through the walkthrough using the designer and look at the code that it produces as a sample of how you could do it by hand. You could then adapt the example as needed for your purposes. For more references try googling "master detail view."
well i would use wpf with a stackpanel of listboxes
the rows are dynamically added to the stackpanel.
the listboxes contain textfields that are databind -ed to mouseclickevents and onchanged events.
http://dotnetslackers.com/articles/silverlight/WPFTutorial.aspx

Trying to reap the benefits of the Rich WEB.UI.Gridview control in a C# application

I know that I can't use this web control in my C# windows application and that I am restricted to DataGridView control. But this does not display results in an elegant manner as the Web.UI.Webcontrol.GridView control but instead displays it like the results when a SQL query is executed in sql server.
I prefer not going down the path of hosting a webuser control which in turn would launch my website using the Web.UI.WebConntrol.GridView control.
You can make the DataGrids look a lot nicer than the default, this MSDN article explains some methods you can use, mostly by changing the available properties, and making use of the DataGridTableStyle and DataGridColumnStyle.
If you want to implement paging, and are working with a read only dataset, you can look at this knowledge base article.
Finally, if you've got money to spnd, there are many companies selling a version of a datagrid control that will look more like the web control, Syncfusion and DevExpress are just two, although I've not used them myself, so can't say how good they are

Programmatically start UserControlTestContainer.exe

I implemented a C# user control and succesfully tested it with the UserControlTestContainer.exe by manually interacting with it. Now I would like to programmatically, instead of manually, feed data into the control, through my unit tests and then automatically display the control filled with that data. I guess I need to create an instance of the control, fill it up and then programatically start the container with the control as parameter, don't I? How do I succeed displaying my filled control? The solution doesn't have to involve the above mentioned container of course, any other suggestion to get the work done would be very appreciated.
I just created an other "Unit Test" project doing all UserControlTestContainer work. UserControlTestContainer is started through the external application option on the project's properties.

Categories

Resources