|
Hi,
welcome to CodeProject.
You have a chicken-and-egg problem here: you are populating the ComboBox inside its SelectedIndexChanged handler, however without any content, the index can't change. You probably want to populate the CB only once, maybe in the Form.Load handler; and not in the SelectedIndexChanged handler: if it takes 1 second to execute, the user would have a hard time scrolling through the CB list.
PS: next time please choose an informative subject line, and put your code snippet in PRE tags (not CODE tags) to get better readability.
Cheers
|
|
|
|
|
I dont want to have to type this out every time I want to connect to the database. Can I put this in a class or something?
What would be the best way to do this so I can call myConnection from anywhere inside my application?
SqlConnection myConnection = new SqlConnection("user id=username;" +
"password=password;server=serverurl;" +
"Trusted_Connection=yes;" +
"database=database; " +
"connection timeout=30");
Thanks in advanced as I am pretty much learning.
|
|
|
|
|
Write a class that encapsulates it, like my DatabaseAccessor[^].
Then create a Data Access Layer that your application can use.
Furthermore: Your application shouldn't know what database is in use. You should not find yourself instantiating Connections, Commands, etc. in your application at all.
modified on Sunday, May 2, 2010 4:04 PM
|
|
|
|
|
A fairly common way to do this is to store the connection string in a static class. Here's a sample that might give you an idea of what I mean:
public static class Settings
{
private static string _connectionString = string.Empty;
public static string ConnectionString
{
get
{
if (string.IsNullOrEmpty(_connectionString))
_connectionString = ConfigurationManager.ConnectionStrings["connection_string_stored_in_config_file"].COnnectionString;
return _connectionString;
}
}
} This supposes that you put the connection string in a configuration file rather than hard coding in your application. Then, in any place you want to call it, you would use it like:
using (SqlConnection myConnection = new SqlConnection(Settings.ConnectionString))
{
} BTW - you should always look at trying to wrap disposable classes in a using block; hence the wrap around the SqlConnection .
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
modified on Monday, May 3, 2010 8:33 AM
|
|
|
|
|
And when you have several different connection strings? And various database engines to use? And desire database agnosticism?
|
|
|
|
|
I wouldn't be tackling this as a beginner. I'd also use something like nhibernate.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Pete O'Hanlon wrote: wouldn't be tackling this as a beginner
Why not? I did. Well, an ADO.net beginner anyway, with ten years experience with embedded SQL and scars from ODBC.
|
|
|
|
|
Yeah, but you've got the yellow diamond.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
|
Ok so this is my class called settings.cs I dont know where I am going wrong but it does not like the code it gives me the following errors:
Error 1 Invalid token '{' in class, struct, or interface member declaration
Error 2 Invalid token '{' in class, struct, or interface member declaration
Error 3 Invalid token '.' in class, struct, or interface member declaration
there is a total of 15 errors any help would be greatly appreciated!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApplication
{
public static class Settings
{
private static string _connectionString = string.Empty;
public static ConnectionString
{
get
{
if (string.IsNullOrEmpty(_connectionString))
_connectionString = ConnectionManager.ConnectionStrings["connection_string_stored_in_config_file"];
return _connectionString;
}
}
}
}
|
|
|
|
|
Terence van Schalkwyk wrote: public static ConnectionString
should read
public static string ConnectionString
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
thanks that fixed it!
Last question:
_connectionString = ConnectionManager.ConnectionStrings["connection_string_stored_in_config_file"];
where do you specify ConnectionManager?
Thanks again!
|
|
|
|
|
Sorry, that should have read ConfigurationManager.ConnectionStrings; add a reference to System.Configuration . This is what happens when I write code directly on the Code Project web site. I'll edit the original code to show what it should be.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I am using visual studio 2008....
Thanks for that!!! I hope this is the last question!!! Its still not liking ConfigurationManager. Does not exist in current context. Am I supposed to initialise this somewhere?
Also in this part of the code "connection_string_stored_in_config_file" is this the section in the config file?
Here is how my code looks:
using System.Configuration;
namespace testapp
{
public static class Settings
{
private static string _connectionString = string.Empty;
public static string ConnectionString
{
get
{
if (string.IsNullOrEmpty(_connectionString))
_connectionString = ConfigurationManager.ConnectionStrings["connection_string_stored_in_config_file"];
return _connectionString;
}
}
}
}
Here is my App.config file. Is this correct so far and how do I put my connection string in?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyConnection" connectionString=""></add>
</connectionStrings>
</configuration>
modified on Monday, May 3, 2010 7:00 AM
|
|
|
|
|
Terence van Schalkwyk wrote: Its still not liking ConfigurationManager
You need to add a reference to System.Configuration. It's not included in your list of references - right click on your reference, select Add reference and choose it from the .NET tab.
Terence van Schalkwyk wrote: _connectionString = ConfigurationManager.ConnectionStrings["connection_string_stored_in_config_file"]
Now that we know what you are going to call your connection string, change this line to:
_connectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString; The connection string you add in here largely depends on the database type and connection details. There is a handy online resource that shows many different types of connection string - it's available here[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
hi
i want write one program in c#
i have very images in this program and i want user can not copy my pictures load in my project ,
with print screen button or other softwares like snagit
please help !!!!
thanks
|
|
|
|
|
Can't be done. Every file can be copied and anything you put on the screen can be captured by any decent screen capture prog.
|
|
|
|
|
Dave Kreskowiak wrote: anything you put on the screen can be captured
For some reason, I have trouble capturing frames from video that plays on my computer. I wonder if that could be utilized to prevent a screen capture of an image. I think it has something to do with hardware acceleration, though it may be a quirk of my video card.
Dave Kreskowiak wrote: Every file can be copied
Embedding them and encrypting them could make it at least difficult to copy files.
|
|
|
|
|
In your video case it might be because the video player is using DirectX. There are programs out there that can grab DirectX output images. You just need to do a search on that. You may want to take a look at this article: Various methods for capturing the screen[^]
|
|
|
|
|
loyal ginger is called: loyal ginger
Prejudice
(watch the whole thing, as it's not what it seems when it starts)
|
|
|
|
|
|
PrtScrn hes problem capturing DirectX video as well as otehr windows that use transparency. PrtScrn sucks but is easily replaced by other screen capturing apps that do a much better job and don't have the limitations of PrtScrn.
aspdotnetdev wrote: Embedding them and encrypting them could make it at least difficult to copy files.
Copying the files is easy. Decrypting the data is harder.
|
|
|
|
|
Dave Kreskowiak wrote: Copying the files is easy. Decrypting the data is harder.
Lol, true.
|
|
|
|
|
aspdotnetdev wrote: have trouble capturing frames from video that plays on my computer
Yeah that'll be the hardware acceleration, what windows actually puts up is some predefined overlay colour and the graphics card graciously fills this colour in with the video as it passes through. So when you take a screen capture you just capture a dark purple block.
If you find what the overlay colour is you can fill a block of it in inside ms paint, and watch any currently playing videos on it.
My current favourite word is: Smooth!
-SK Genius
|
|
|
|
|
SK Genius wrote: If you find what the overlay colour is you can fill a block of it in inside ms paint, and watch any currently playing videos on it.
I always wondered why it did that. Thanks for the info!
|
|
|
|