|
Can you tell me how to use the connection string value from the app.config file and not the hard codee values?
In a C# 2008 desktop/console application, I created the connections to the database using linq to sql. Everything worked fine until I moved the
code to point to a different database. Somehow the original database connections are being saved and the values from the app.config file
are not being used.
I tried to do what the following link: http://refat38.wordpress.com/2012/01/30/unpick-linq-to-sql-connection-string-from-application-settings/,
said to do however, I do not know what the following statement means to do from this link:
Now Right Click and open the Properties on your DAL or project containing your LINQ to SQL classes and remove the connection string “Application Setting” reference on the Settings tab.
I also tried to add the following to the *.designer.cs file:
base(ConfigurationManager.ConnectionStrings["esample.Properties.Settings.devtestConnectionString"].ConnectionString, mappingSource) and attached
a system.configuration file as a reference.
1. The following code is from the app.config file:
<connectionStrings>
<add name="esample.Properties.Settings.devtestConnectionString"
connectionString="Data Source=dev;Initial Catalog=devtest;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
2. The following is from the settings.settings file
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="esample.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="devtestConnectionString" Type="(Connection string)" Scope="Application">
<DesignTimeValue Profile="(Default)"><?xml version="1.0" encoding="utf-16"?>
<SerializableConnectionString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ConnectionString>Data Source=dev;Initial Catalog=devtest;Integrated Security=True</ConnectionString>
<ProviderName>System.Data.SqlClient</ProviderName>
</SerializableConnectionString></DesignTimeValue>
<Value Profile="(Default)">Data Source=dev;Initial Catalog=devtest;Integrated Security=True</Value>
</Setting>
</Settings>
</SettingsFile>
The following is from the settings.designer.cs file
namespace esample.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
[global::System.Configuration.DefaultSettingValueAttribute("Data Source=dev;Initial Catalog=devtest;Integrated Security=True")]
public string devtestConnectionString {
get {
return ((string)(this["devtestConnectionString"]));
}
}
}
}
The following is from the *.designer.cs
namespace esample
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
using System.Configuration;
[System.Data.Linq.Mapping.DatabaseAttribute(Name="devtest")]
public partial class esampleDataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
public esampleDataContext() :
base(global::esample.Properties.Settings.Default.devtestConnectionString, mappingSource)
{
OnCreated();
}
public esampleDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
Can you show me code and/or tell me how to solve this problem so I can obtain the value from the app.config file?
|
|
|
|
|
If you have the database code in a different project than the main project, the connection string could have been saved in that project, but is still using the app.config in the main project. I know I have seen this in Entity Framework.
|
|
|
|
|
If that is the case, how do you fix the problem?
|
|
|
|
|
You edit the app.config that's in the main project.
|
|
|
|
|
As the man stated, copied and pasted the connection string from the library project to the main project.
|
|
|
|
|
if I remember correctly:
Find your "context" class. Propably something named (your DB name)DataContext. Press F12 to find file with it. It is autogenerated. Connection string is saved either as Attribute on that class or as string in 1 of constructors. Delete it.
This should work but if your app does not work I bet there is something wrong also. Like connection strings names differ.
No more Mister Nice Guy... >: |
|
|
|
|
|
Ive used a couple of the GoF pattens in my applications as well as read articles and books on them, i however want to find out from the rest of you guys where you used the patterns before?
So, if you could list examples or short descriptions where you have used the various Behavioral, Creational and Structural patterns in real production applications that would be awesome!
|
|
|
|
|
Pick a forum and stick with it. Please.
|
|
|
|
|
Gohan_Coder wrote: So, if you could list examples or short descriptions where you have used the various Behavioral, Creational and Structural patterns in real production applications that would be awesome!
No, it wouldn't be awesome, it'd be a friggin' waste o' time without an apparent goal, or advantage on my side. When talking to an engineer, drop the marketing-fluff in your texts.
This post would cause a timeout if I were to list examples for each pattern; if you're having trouble imagining a use for them, then you lack experience in "writing" code. It does not mean you did not study hard, only that you did not yet run into the problems that they're supposed to solve.
To answer in your style; "Have a great day!"
If that sounded like an insult, go for programming. Otherwise, go for marketing
|
|
|
|
|
Searching through the article section on this site will provide with you a number of articles on the design patters that you have implemented yourself.
|
|
|
|
|
For websites, I've just MVC and MVVM quite happily. With MVC, I've both done ASP.NET MVC and also used ExtJS, by Sencha, which suits this design well. For web MVVM, I use Knockout.js. As with everything else, I've mixed patterns together, so I've used Factory patterns, observables, lazy patterns, object pools, bridges, composition, adapters, decorators and many other patterns.
For desktop apps, I heavily rely on MVVM and the Mediator, along with many of the other patterns I've listed above - and many, many others.
The point to understand is, you don't write your application to use a pattern, you use a pattern to support features of your application. In other words, don't try to slavishly use patterns in your application - just use the tools that are necessary to solve the problem at hand. You'll be surprised how many patterns you use on a day to day basis without even knowing it.
|
|
|
|
|
Why does my attempt to use a method called "Start_Data_Command_04" in this file produce the stated error ? (I'll list the error immediately after the code)
File Name: Form1.cs
namespace George_Washington
{
public partial class Form1 : Form
{
void Button4_Start_Data_Click(object sender, EventArgs e)
{
The_User_Clicked_A_Button_Called.Start_Data_Command_04();
}
}
}
Quote: Error 2 An object reference is required for the non-static field, method, or property 'SerialPortPractice_03.The_User_Clicked_A_Button_Called.Start_Data_Command_04()'
The method named "Start_Data_Command_04" is located in this file, and produces an error (no clue if they are related) of its own...
File name: The_User_Clicked_A_Button_Called.cs
namespace George_Washington
{
public class The_User_Clicked_A_Button_Called
{
internal void Start_Data_Command_04()
{
UART.send_command
( Protocol_Packets_01.BARN_Command_04_Start_Data_Flood,
0,
Protocol_Packets_01.BARN_Command_04_Start_Data_Flood.Length
)
;
}
}
}
Quote: Error 1 An object reference is required for the non-static field, method, or property 'SerialPortPractice_03.UART.send_command(byte[], int, int)'
No matter what word I put in front of the method; e.g., internal, private, public, static, whatever; I still get an error.
The actual method in question is here
File Name: UART.cs
namespace George_Washington
{
class UART
{
public void send_command(byte[] The_Byte_Array, int Starting_Place, int Length)
{
The_Serial_Port_We_Are_Testing.Write(
The_Byte_Array,
Starting_Place,
Length
)
;
}
}
}
|
|
|
|
|
The_User_Clicked_A_Button_Called.Start_Data_Command_04(); This statement is telling the compiler that you have a static method called Start_Data_Command_04 in the class The_User_Clicked_A_Button_Called . Plainly, from the definition of this method, this is not right - this method is actually an instance method. So, you either have to declare an instance of The_User_Clicked_A_Button_Called using new The_User_Clicked_A_Button_Called().userClicked.Start_Data_Command_04(); or you have to add the static modifier to Start_Data_Command_04 .
|
|
|
|
|
Thank you, a lot.
When I change this line...
internal void Start_Data_Command_04()
...to this line...
internal static void Start_Data_Command_04()
...then the second error ("Error 1" in my initial example) goes away, but the first one ("Error 2") remains.
My attempts to declare an instance of the method produced more compiler errros.
If you (or anyone who's reading) could tell me the syntax of how to declare an instance of the method in question, I thank you.
More importantly, and an even greater value, would be to show me how to find the syntax for myself.
Performing five or ten different searches on Google and Bing, (and MSDN, which is really Bing, as I understand it) using my best guess of how to phrase it, (e.g., with search phrases like, "C#, The specific syntax for Declaring an instance of a static method in another class" and so on) I did not immediately see an example of the syntax for declaring an instance of a static method in another class.
|
|
|
|
|
The_User_Clicked_A_Button_Called anInstnace = new The_User_Clicked_A_Button_Called(); That's how you declare an instance. In order to call the method using an instance, the method must NOT be static. As far as mastering the concepts behind C#, I would really recommend this[^] book.
[Edit note]Please change your class and method names to be less cumbersome. You'll thank me in the long run.
|
|
|
|
|
You have a set of extra parenthesis in there pete.
|
|
|
|
|
Cheers mate - it's such a stupid class name, I just copied and pasted it from my original answer. I should have checked what I picked up there.
|
|
|
|
|
I second that book recommendation.
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
This is actually meant as a comment.
It appears that you are using a very non-standard naming convention in your code. This actually makes the code much more difficult for a another developer to read as we all become accustomed to specific casing and naming methodologies. It's all good if this is just something you are writing for yourself, but habits are hard to break and if you work on commercial code with naming schemes like this, you may run into some developers who won't take kindly to it.
You should take a look at Camel Casing[^]. This is the most common way that CSharp developers write code, and if used correctly will be much appreciated by other developers who will have to look at your code sometime. I have taken the liberty of taking one of your methods above to show you what I mean. Cheers.
namespace GeorgeWashington
{
public class TheUserClickedAButtonCalled
{
internal void StartDataCommand04()
{
UART.send_command
( ProtocolPackets01.BARN_Command04StartDataFlood,
0,
ProtocolPackets01.BARNCommand04StartDataFlood.Length
)
;
}
}
}
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Just read your suggested webpage.
If the underscores are annoying, then I'll do my best to remove them and Smush the words together.
My vocabulary now includes the phrase CamelCasing.
Oh, and thanks for the input.
|
|
|
|
|
Hi.
I have an application that store PDF file in a library and do some other stuff like filtering, searching ...
what i want to do is the next:
when the user hit any pdf file in the windows explorer with the left mouse button, I want to add a menu item to that context menu (of the operating system)"Add this book to library for example", so that i can add this file to my app data base without openning the whole application,(using another sub program that took the path of this file).
is there any way to do this please using C#.
|
|
|
|
|
"The Operating System"?
This is hard enough in Windows, let alone you venture out to other platforms! Extending the system is not what a typical application does, hence it's not part of the framework. You'd need to write a Shell Extension[^], which has become a bit harder since the introduction of 64-bit machines.
|
|
|
|
|
Thanks Eddy Vluggen, that was very helpfull.
|
|
|
|
|
You're welcome 
|
|
|
|
|
I'm new to writing code of any sort. I'm taking classes to eventually be a .NET SharePoint designer.
1. Do any of you have tips regarding what I need to remember and what I can simply pull up from Visual Studio?
2. Do any of you have suggestions regarding the best way to begin remembering code? 3x5 cards, etc...?
3. Any suggestions regarding websites to access, good video's, etc... to help with my learning the necessary?
Thanks!
Maurice
Maurice Lantier
Fort Worth, Texas
|
|
|
|
|