In my bllLanguage.cs class I am not able to create dalLanguage class's objects and vice versa. It says dalLanguage.cs/bllLanguage.cs could not be found.
Whats wrong with the code below?
bllLanguage.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Proj2;
namespace Proj2.BLL.Main.Setting
{
public class bllLanguage
{
public bllLanguage()
{
//add constructor code here
}
#region Properties
/// <summary>
/// Properties
/// </summary>
private int intLanguageID;
private string strDescription;
private string strValue;
#endregion
public int LanguageID
{
get { return intLanguageID; }
set { intLanguageID = value; }
}
public string Description
{
get { return strDescription; }
set { strDescription = value; }
}
public string Value
{
get { return strValue; }
set { strValue = value; }
}
#region getLanguage
/// <summary>
/// getLanguage
/// </summary>
/// <returns></returns>
public DataSet getLanguage()
{
dalLanguage objdalLanguage = new dalLanguage(); // ERROR HERE
DataSet dsgetLanguage = objdalLanguage.getLanguage();
return dsgetLanguage;
}
#endregion
}
}
dalLanguage.cs
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Data.SqlClient;
using System.Web;
using Microsoft.ApplicationBlocks.Data;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Proj2;
namespace Proj2.DAL.Main.Setting
{
public class dalLanguage
{
public dalLanguage()
{
//constructor code here
}
#region getLanguage
/// <summary>
/// getLanguage
/// </summary>
/// <returns></returns>
public DataSet getLanguage()
{
DataSet dsgetLanguage = new DataSet();
try
{
dsgetLanguage = SqlHelper.ExecuteDataset(Constants.ConnectionString, CommandType.StoredProcedure, "[Main].[sp_getLanguage]");
}
catch (Exception ex)
{
throw ex;
}
return dsgetLanguage;
}
#endregion
}
}
the namespaces do not match
Proj2.BLL.Main.Setting
Proj2.DAL.Main.Setting
you need to specify the fully qualified name or import the namespaces. importing Proj2 only is not enough
First thing I noticed is that you lack using statement in bllLanguage.cs. add
using Proj2.DAL.Main.Setting;
in bllLanguage.cs
First of never write
catch (Exception ex)
{
throw ex;
}
Throw ex will overwrite the stacktrace!
Write just throw if you want to re throw the exception.
Question are they in the same project and if not do they have a reference to the other project?
Related
I want to autoscroll WPF ListBox to bottom automatically. I have two classes - one is Timer.cs and another one is MainWindow.xaml.cs
Here is Timer.cs:
using System;
using System.Configuration;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Importer_WPF
{
class Timer
{
public static readonly string MinutesExecution = ConfigurationManager.AppSettings["MinutesExecution"];
static System.Threading.Timer timer;
public static void StartTimer()
{
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(Convert.ToDouble(MinutesExecution));
timer = new System.Threading.Timer((e) =>
{
Task.Delay(100).ContinueWith(_ => App.Current.Dispatcher.Invoke(() => MainWindow.Names.Add(DateTime.Now.ToString())));
MainWindow.AutoScroll(); // Problem is here
}, null, startTimeSpan, periodTimeSpan);
}
public static void StopTimer()
{
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
}
}
MainWindow.xaml.cs:
using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
namespace Importer_WPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static ObservableCollection<string> Names { get; set; }
public static bool IsCheckedYes { get; set; }
[Obsolete]
public MainWindow()
{
InitializeComponent();
}
public void AutoScroll()
{
int itemCount = ConsoleOutput.Items.Count - 1;
if (itemCount > -1)
ConsoleOutput.ScrollIntoView(ConsoleOutput.Items[itemCount]);
}
}
}
Debugger is giving this message:
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field,
method, or property 'MainWindow.AutoScroll()'
Any hints how to edit code structure so it will not produce errors?
You need to get a reference to the instance of mainwindow class which is in memory.
((MainWindow)Application.Current.MainWindow).AutoScroll();
A class named Round is a level design in a adventure game.Its filed number indicate which level the player is in. Different level will have different figures to guess. The figures are produced by field FigureFactory.
The question is: using Ninject for dependency injection, how can I set the cooresponding FigureFactory to the variable round, according to the field number? For instance, when field number==1, the cooresponding factory is FigureFactory1, when field number==2, the cooresponding factory is FigureFactory2?
using GuessFigure.Model.Factory;
using Ninject;
using Ninject.Modules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ninject.Planning.Bindings;
namespace GuessFigure.Model
{
class Round
{
private int number=1;
private FigureFactory figureFactory;
[Inject]
internal void SetFigureFactory(FigureFactory figureFactory)
{
this.figureFactory = figureFactory;
}
public int[] GetCurrentRoundFigures()
{
return figureFactory.Produce(number);
}
}
//this not work, help please
class RoundModule : NinjectModule
{
public override void Load()
{
Bind<FigureFactory>().To<FigureFactoryRound1>().When(request=>request.ParentRequest.Target.Type.GetField("number").Equals(1));
Bind<FigureFactory>().To<FigureFactoryRound2>().When(request => request.Target.Type.GetField("number").Equals(2));
Bind<FigureFactory>().To<FigureFactoryRound3>().When(request => request.Target.Type.GetField("number").Equals(3));
Bind<FigureFactory>().To<FigureFactoryRound4>().When(request => request.Target.Type.GetField("number").Equals(4));
Bind<FigureFactory>().To<FigureFactoryRound5>().When(request => request.Target.Type.GetField("number").Equals(5));
}
}
}
Factory Method Pattern implementation:
using System;
namespace GuessFigure.Model
{
abstract class FigureFactory
{
protected int figureNumber;
public FigureFactory(int figureNumber)
{
this.figureNumber = figureNumber;
}
internal int[] Produce()
{
int[] figureArray = new int[figureNumber];
for (int i = 0; i < figureNumber; i++)
{
figureArray[i] = Algorithm(i + 1);
}
return figureArray;
}
abstract protected int Algorithm(int inputNumber);
}
}
Concrete Factory(there are still some like this);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessFigure.Model
{
class FigureFactoryRound1 : FigureFactory
{
public FigureFactoryRound1(int figureNumber) : base(figureNumber)
{
}
protected override int Algorithm(int inputNumber)
{
return inputNumber;
}
}
}
class FigureFactoryRound3 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessFigure.Model.Factory
{
class FigureFactoryRound3 : FigureFactory
{
public FigureFactoryRound3(int figureNumber) : base(figureNumber)
{
}
protected override int Algorithm(int inputNumber)
{
return (int) Math.Pow( inputNumber,2) ;
}
}
}
class FigureFactoryRound4 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GuessFigure.Model.Factory
{
class FigureFactoryRound4 : FigureFactory
{
public FigureFactoryRound4(int figureNumber) : base(figureNumber)
{
}
protected override int Algorithm(int inputNumber)
{
return (int)Math.Pow(inputNumber, 3);
}
}
}
Usage:
IKernel kernel = new StandardKernel(new RoundModule());
Round round = new Model.Round();
round.SetFigureFactory(kernel.Get<FigureFactory>());
int[] array=round.GetCurrentRoundFigures();
I am using Ninject as dependency injection tool. I have quite big asp.net-mvc 5 project (80 bindings) which, when I step through my code creates the bindings perfectly in the kernel.The NinjectWebCommon.cs. file sporadically failed on the following line of code
(kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);)
resulting in a new kernel being created and me losing all bindings to my SQL Server database.
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
AddBindings(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
The result is that when I execute the code, I get a NULL model error in the view. The error has corrected itself automagically but I need to know what causes it so that I can rather deal with it. I include the full NinjectWebCommon.cs file
using eMedic.Domain.Abstract;
using eMedic.Domain.Concrete;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(eMedic.WebUI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(eMedic.WebUI.App_Start.NinjectWebCommon), "Stop")]
namespace eMedic.WebUI.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
AddBindings(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
private static void AddBindings(IKernel kernel)
{
// add bindings here
kernel.Bind<IAccountRepository>().To<EFAccountRepository>();
kernel.Bind<IAddressRepository>().To<EFAddressRepository>();
kernel.Bind<IAddressTypeRepository>().To<EFAddressTypeRepository>();
//other bindings as well (omitted for brevity)
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
System.Web.Mvc.DependencyResolver.SetResolver(new eMedic.WebUI.Infrastructure.NinjectDependencyResolver(kernel));
}
}
}
As well as the NinjectDependencyResolver.cs class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ninject;
using eMedic.Domain.Abstract;
using eMedic.Domain.Concrete;
namespace eMedic.WebUI.Infrastructure
{
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
}
}
The data interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace eMedic.Domain.Abstract
{
public interface IAddressRepository
{
IEnumerable<address> Address { get; }
}
}
and the other data interface...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using eMedic.Domain.Abstract;
namespace eMedic.Domain.Concrete
{
public class EFAddressRepository:IAddressRepository
{
private EFDbContext context = new EFDbContext();
public IEnumerable<address> Address
{
get { return context.address; }
}
}
}
I really would appreciate assistance so that I can get to the root cause of the error.
I managed to figure out what the problem was and for reference I add the reason here:
I had an error with a foreign key (FK) value in one of the tables I was referencing in my SQL Server database. It had a NULL value and when I changed the record to give that specific FK in that specific table a value, the error was cleared.
I want to call the method from the codebehind of a window that is NOT the MainWindow in my WPF application, casting the window type as I do it.
ClientCallBack.cs:
using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace ChatClient
{
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ClientCallback : IClient
{
public void GetMessage(string message, string userName)
{
//get casted instance of chat client window (NOT MainWindow!)
}
}
}
ChatWPFClient.xaml.cs:
using ChattingInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace ChatClient
{
/// <summary>
/// Interaction logic for ChatWPFClient.xaml
/// </summary>
public partial class ChatWPFClient : Window
{
public static IChattingService Server;
private static DuplexChannelFactory<IChattingService> _channelFactory;
public ChatWPFClient()
{
InitializeComponent();
_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(), "ChattingServiceEndpoint");
Server = _channelFactory.CreateChannel();
}
private void sendMessage(object sender, RoutedEventArgs e)
{
MessageBox.Show("Not available yet!");
}
public void TakeMessage(string message, string userName)
{
chatBox.Text += userName + ": " + message + "\n";
}
}
}
How can I call the TakeMessage of this method in the other class so I can use that codebehind window to populate the XAML file for ChatWPFClient.xaml? Thanks in advance!
First create an interface that you can pass to the ClientCallback
public interface IMessageHandler
{
void TakeMessage(string message, string userName);
}
Then in the ClientCallBack take the interface as a parameter in the constructor.
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class ClientCallback : IClient
{
private IMessageHandler messageHandler;
public ClientCallBack(IMessageHandler messageHandler)
{
this.messageHandler = messageHandler;
}
public void GetMessage(string message, string userName)
{
messageHandler.TakeMessage(message, userName);
}
}
Use the interface for the ChatWpfClient and pass the instance in the constructor.
public partial class ChatWPFClient : Window, IMessageHandler
{
...
public ChatWPFClient()
{
InitializeComponent();
_channelFactory = new DuplexChannelFactory<IChattingService>(new ClientCallback(this), "ChattingServiceEndpoint");
Server = _channelFactory.CreateChannel();
}
...
// This is a part of the interface now and needs to be implemented here
public void TakeMessage(string message, string userName)
{
chatBox.Text += userName + ": " + message + "\n";
}
}
Also you could just implement the IClient on your ChatWPFClient class and decorate with the CallBackBehavior attribute and just pass itself as the callback. But don't think this is recommended, seems weird.
_channelFactory = new DuplexChannelFactory<IChattingService>(this, "ChattingServiceEndpoint");
I have a problem that has been driving me nuts.
I have 2 ASPX pages in which the parent use Server.Transfer() function. The parent is called Submit.aspx whereas child is called Review.aspx
In Submit.aspx.cs, I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Contacts_Submit : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Review_Click(object sender, EventArgs e)
{
Server.Transfer("Review.aspx", true);
}
/*
* Sequence of functions that will server as a Get functionality that will
* return the text inside each textbox.
* These information will be used by "Review.aspx" to validate the
* information given by the user before final submission takes place.
*/
public string GetFirstName { get { return FirstName.Text; } }
public string GetLastName { get { return LastName.Text; } }
public string GetAddress { get { return Address.Text; } }
public string GetCountry { get { return Country.SelectedValue; } }
public string GetProvince { get { return Province.SelectedValue; } }
public string GetCity { get { return City.Text; } }
public string GetZipCode { get { return ZipCode.Text; } }
public string GetWorkPhone { get { return WorkPhone.Text; } }
public string GetMobilePhone { get { return MobilePhone.Text; } }
public string GetFax { get { return Fax.Text; } }
public string GetEmail { get { return Email.Text; } }
public string GetCompany { get { return Company.Text; } }
public string GetWebsite { get { return Website.Text; } }
public string GetRelationship { get { return Relationship.SelectedValue; } }
}
Whereas on the Review.aspx.cs, I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Contacts_Review : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if(PreviousPage != null)
{
Contacts_Submit prevpage = PreviousPage as Contacts_Submit;
//FirstName.Text = PreviousPage.GetFirstName;
}
}
}
The problem is when I declare "Contacts_Submit prevpage = PreviousPage as Contacts_Submit". The system is giving me an error that says "The type or namespace name 'Contacts_Submit' could not be found (are you missing a using directive or an assembly reference?)".
I am a beginner in both ASP.NET and C#, can anyone help me with this? Thank you SOOO MUCH.
I think you just want
Contacts_Submit prevpage = PreviousPage as Contacts_Submit;
instead of
Contacts_Submit prevpage = PreviousPage as System.Data.DataSet Contacts_Submit;
Contacts_Submit is type of Page and not in any way related to Dataset, so your cast is invalid.
remove that and it should be fine