I am trying to set my config from code according to this post Changing .net Membership ConnectionString
In this post, it has this code:
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
// Get your new connnection string name and then overwrite the base here:
config.Item["connectionStringName"] = "MyNewConnectionStringName";
base.Initilize(name, config);
}
I made my class as such:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.Security;
using System.Collections.Specialized;
namespace WebUsers
{
internal class MembershipOverride : SqlMembershipProvider
{
public override void Initialize(string name, NameValueCollection configx)
{
// Get your new connnection string name and then overwrite the base here:
configx.Item["connectionStringName"] = "MyNewConnectionStringName";
base.Initilize(name, config);
}
}
}
I cannot get Item or Initialize to be recognized.
It is doing this:
Anyone see what I am doing wrong?
I cannot get Item or Initialize to be recognized.
There is a typo. You call base.Initilize instead of base.InitiAlize.
To access the collection element, try just
configx["connectionStringName"] = "MyNewConnectionStringName";
Related
Edit 2, I'll Rephrase the whole post:
I have the static class Project.Shared.Constants and it has some constants and public getter properties:
using System;
using System.IO;
using Microsoft.Win32;
namespace Project.Shared
{
public static class Constants
{
public const RegistryHive HIVE = RegistryHive.LocalMachine;
/*
... more constants
*/
public static RegistryView RegistryView
{
get
{
return Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
}
}
}
}
Then I had a Project.Infrastructure.RegistryReader. It exists in the code for quite some time and uses the static class.
using System;
using System.IO;
using Microsoft.Win32;
using Project.Shared;
namespace Project.Infrastructure
{
public class RegistryReader
{
public string Read(string keyName)
{
using var baseKey = RegistryKey.OpenBaseKey(Constants.HIVE, Constants.RegistryView);
using var subRegistryKey = baseKey.OpenSubKey(keyName);
/*
Actual access, read, parse
*/
return resultValue;
}
}
}
Now I want to add a Project.Infrastructure.RegistryWriter in the same project and same namespace and use the same statics:
using System;
using System.IO;
using Microsoft.Win32;
using Project.Shared;
namespace Project.Infrastructure
{
public class RegistryWriter
{
public void WriteEthernetChannelInRegistry(string keyName, string value)
{
using var baseKey = RegistryKey.OpenBaseKey(Constants.HIVE, Constants.RegistryView);
using var subRegistryKey = baseKey.OpenSubKey(keyName, writable: true);
/*
Actual writing of the registry key
*/
}
}
}
Locally it works, so it can't be totally wrong. On the build system it fails, but only with the Writer. If I declare the same property locally, then the build finishes and has no issues with the Reader.
It might be something between VS using msbuild and the build system using dotnet build --Release but I couldn't make it fail locally yet.
Edit 3, the error I receive is:
Error: RegistryWriter.cs(72,99): error CS0117: 'Constants' does not contain a definition for 'RegistryView' [c:\source\...\Project.Infrastructure.csproj]
I can't seem to figure out what is going on here.
I have posted below my code and a photo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace test
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
ChromeOptions options = new ChromeOptions();
options.AddExtensions(new File("pathtoextentions.crx"));
IWebDriver driver = new ChromeDriver(options);
//Test Opening Website
driver.Navigate().GoToUrl("https://google.com");
}
}
}
I have also uploaded a photo here where it show my error is on new File: https://ibb.co/Lk72dsD
Error CS0712 Cannot create an instance of the static class 'File'
What am I doing wrong?
File is a static class, you can't create instances of it like new File().
Looking at ChormeOptions.AddExtensions, it accepts an array or an IEnumerable of string.
So, we could use an array in your case. To do so, replace this:
options.AddExtensions(new File("pathtoextentions.crx"));
with this:
options.AddExtensions(new[] { "pathtoextentions.crx" });
Solution Explorer
I can get current method name, but currently I can't get really current class name.
From reference: C# getting its own class name, accepted answer is this.GetType().Name.
Test.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string currentMethodName = MethodBase.GetCurrentMethod().Name; // "Page_Load"
string currentClassName1 = this.GetType().Name; // "test_test_aspx" // How to get "Test" only?
string currentClassName2 = this.GetType().FullName; // "ASP.test_test_aspx" // How to get "Test" only?
}
}
}
Try with this:
string className = MethodBase.GetCurrentMethod().DeclaringType.Name;
A bit rusty here with regards to WCF Services.
I have a custom class named cSecurity.cs which does some custom functions.
I want to use this custom class inside my Service:
This is the App.svc.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace AppServices
{
public class App : IApp
{
public cSecurity _csec;
public string GetItems(int agentID, string agentName)
{
// Need to use some functions from the cSecurity class here???
return _csec.getItems();
}
}
}
This is the cSecurity.cs class:
using System.Collections;
using System.Configuration;
using System.Net;
namespace AppServices
{
public class cSecurity
{
// Some functions defined here....
public string getItems(){
return string.Empty;
}
}
}
But I am getting an error on the line:
public cSecurity _csec;
"The type or namespace name 'cSecurity' could not be found."
This seems pretty trivial but I seem to be lost here.
Appreciate any insight. Thanks.
For some reason, the solution for this was for me to close and restart VS. Not sure what caused the class for it to be not referenced by VS.
I am trying to access a global variable that was set in my Global.asax section of my project. When include the namespace in my DAL and access the variable visual studio complains. Here is the code
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Application["NHSessionFactory"] = CreateSessionFactory();
}
And here is my DAL logic
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using NHibernate;
namespace Data.DAL
{
public class DB
{
private ISessionFactory session;
public DB()
{
ISessionFactory session = (ISessionFactory) Application["NHSessionFactory"]; //problems here
}
public ISessionFactory GetSession()
{
return session;
}
}
}
A screen shot of the error:
How would I access that variable that was set in Global.asax?
Yes you can use the Application variables same way as asp.net
Example:
System.Web.HttpContext.Current.Application.Lock();
System.Web.HttpContext.Current.Application["NHSessionFactory"] = "Something";
System.Web.HttpContext.Current.Application.UnLock();