c# class how to reference in .cs file for website - c#

I'm new in programming. I've followed the steps in the book "Pro asp.net 4 in c#2010" to create 2 classes.
Now I try to use this class on a webpage. In the .cs file I added using "DBComponent;" but Visual Studio says "The type or namespace name 'DBComponent' could not be found (are you missing a using directive or an assembly reference?)"
What did I forget?
If i try to add the compiled dll under 'references' i get the error:
The type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs' conflicts with the imported type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\bin\Debug\DBComponent.dll'. Using the type defined in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs'.
This is what I've done:
Create a new empty website 'OSWeb' in VS
On top of this solution I clicked right and choosed: Add > new Project > Visual C# > Windows > Class library: name: DBComponent and I stored in D:_Web\OSWeb\DBcomponent
List item
I created 2 cs files (EventDB.cs and EventDetails.cs)
my events.cs file
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDB
{
private string connStr;
public EventDB()
{...}
public EventDB(string connectionString)
{...}
public int InsertEvent(EventDetails evd)
{...}
}
}
This is my eventdetails.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDetails
{
private int eventId;
private string eventName1;
public int EventId { get { return eventId; } set { eventId = value; } }
public string EventName1 { get { return eventName1; } set { eventName1 = value; } }
public EventDetails(int eventId, string eventName1)
{
this.eventId = eventId;
this.eventName1 = eventName1;
}
public EventDetails() { }
}
}
Now I create a new webpage (this is the .cs file of this webpage) (events.aspx.cs)
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBComponent; => here is the error
public partial class events : System.Web.UI.Page
{
private EventDB db = new EventDB(); => here is the same error
protected void Page_Load(object sender, EventArgs e)
{
}
}

try the following:
right click on your web project and click in properties, then click en references. remove the reference of your class, close this windows. Verify in the Bin folder from you proyect, yours references (should not be the reference has been removed.). Next, you open again the project properties and add your class library.
(before adding the reference should be sure that the class library compiles correctly.)
sorry for my english

Related

Get Logged in username using a function

I have a class with the following function
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
namespace Performance_Dashboard.Controllers
{
public class GetLoggedInData
{
public string ActualUser { get; internal set; }
public string GetLoggedInDataofUser()
{
ActualUser= HttpContext.Current.User.Identity.Name;
return ActualUser;
}
}
}
I would like to access or see what is being returned, how do I do that?
You said
[I set a breakpoint] ... but my program does not go to that function that means that I have to call it right?
The answer is yes. In order to hit the breakpoint you must call the method:
GetLoggedInData glid = new GetLoggedInData();
glid.GetLoggedInDataofUser();//Call the method. Now the breakpoint will be hit
An additional method you can use other than setting a break point is using the System.Diagnostics library to print to the output window.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Diagnostics;
namespace Performance_Dashboard.Controllers
{
public class GetLoggedInData
{
public string ActualUser { get; internal set; }
public string GetLoggedInDataofUser()
{
ActualUser= HttpContext.Current.User.Identity.Name;
Debug.Write(ActualUser);
return ActualUser;
}
}
}

interface with System.IO Object cannot be completed in Xamarin.iOS project

I'm trying to make an interface like this
using System;
using System.IO;
using System.Text;
using System.Threading;
using Xamarin.Forms;
namespace Foo
{
public interface IStreamProvider
{
Stream OutputStream { get; set; }
}
}
and implement it in the iOS project like this:
using System;
using System.IO;
using Foundation;
using Foo;
[assembly: Xamarin.Forms.Dependency(typeof(StreamProvider_iOS))]
namespace Foo.iOS
{
public class StreamProvider_iOS : IStreamProvider
{
public Stream OutputStream { get; set; }
}
}
but I end up getting this error:
'StreamProvider_iOS ' does not implement interface member
'IStreamProvider.OutputStream'. 'StreamProvider_iOS.OutputStream'
cannot implement 'IStreamProvider.OutputStream' because it does not
have the matching return type of 'Stream'.
I'm thinking it's because System.IO in the base xamarin project isn't the same as System.IO in the Xamarin.iOS project, but I'm not really sure and don't know how to work around that if it's the case.

Unable to programmatically set form background image from picture resource

I just want to set the background image of another form to a png file picture resource I added
I have the following in my MAstrategyBldrForm.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MAStrategyBuilder
{
public partial class MAstrategyBldrForm : Form
{
private CrossPicsForm crossPic;
public MAstrategyBldrForm()
{
InitializeComponent();
crossPic = new CrossPicsForm();
}
....
....
.... later in same file:
private void MAcrossPicButton_Click(object sender, EventArgs e)
{
crossPic.BackgroundImage = (Bitmap) (Properties.Resources.ResourceManager.GetObject("Moving Avgs Cross.png"));
crossPic.Show();
}
....
....
In my CrossPicsForm.cs file I have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MAStrategyBuilder
{
public partial class CrossPicsForm : Form
{
public CrossPicsForm()
{
InitializeComponent();
}
}
}
Everything compiles but when I click my button control in the mastrategy... form, a CrossPicsForm window is displayed that is blank. Darn!
You won't get an exception when you mis-spell the resource name, ResourceManager simply returns null and that leaves the BackgroundImage property unchanged. You did, it would normally be named "Moving_Avgs_Cross" without the .png extension.
Do favor using the property that was added by the resource designer instead. That ought to resemble:
crossPic.BackgroundImage = Properties.Resources.Moving_Avgs_Cross;
Use IntelliSense to help you fall in the pit of success here.

How to add namespace into Model MVC

I created folder "App_Code" in my project, with simple class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Tools
{
public static class TextTools
{
public static void GenerateHash(string str, int user)
{
}
}
}
My question is: How can I add this namespace "Tools" with all classes and methods to my project? I tried to insert using Tools; in my AccountModels.cs but it isn't working.
Regards
probably you need to add "Tools" in your reference

bringing two projects together

I have a simple window application I am starting and I was given another file I would like to bring into the new project. How do I bring them both together to work as one file?
New Project
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Other
using System;
using System.Text;
using System.IO;
using System.Net;
namespace CaptchaClient
{
public static class SampleUse
{
public static void SampleSolve()
{
}
}
public class CaptchaSolver
{
}
public enum ResponseState
{
}
}
This screenshots shows the how yo do he procedure
Right click on your project in the Solution Explorer and click Add Existing Item. Then browse to the other class file. Once imported, you might want to change its namespace to the one you are using in your project.

Categories

Resources