Namespace is not working properly - c#

I am getting a error for my namespace that says " the type or namespace name could not be found (are you missing a using directive or an assembly reference?) " and have no idea why... i have been trying to figure this one out for hours now... btw i am a newbie with c#
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using portfolioNamespace;
public partial class DefaultCode : System.Web.UI.Page{
Portfolio myPortfolio;
protected void Page_Load(object sender, EventArgs e){
//declare
myPortfolio = new Portfolio();
populate();
}
private void populate(){
//populate everything
populateMenu();
populateHome();
populateSamples();
populateAbout();
}
private void populateMenu(){
//populate the menu
String[] menu;
menu = myPortfolio.getMyMenu();
repLinks.DataSource = menu;
repLinks.DataBind();
//populates a hidden text box so i can get an array in javascript
for (int i = 0; i<=menu.Length - 1; i++){
dummy.Value = dummy.Value & menu(i) & "-";
}
}
private void populateHome(){
//populate home
homeRepeater.DataSource = myPortfolio.getHomeInfo;
homeRepeater.DataBind();
}
private void populateSamples(){
//populate samples
samplesRepeater.DataSource = myPortfolio.getSamplesInfo;
samplesRepeater.DataBind();
}
private void populateAbout(){
//populate about
aboutRepeater.DataSource = myPortfolio.getAboutInfo;
aboutRepeater.DataBind();
}
}

In your Solution Explorer Right click your References, click Add Reference > Browse, then find your dll file that contains portfolioNamespace and add it to your References.
See documentation for more details: How to: Add or Remove References By Using the Add Reference Dialog Box

You probably haven't created the portfolioNamespace.
You do so by wrapping your code in
namespace portfolioNamespace
{
//your code here
}
Have a read through these references, namespace (C# Reference) and Namespaces (C# Programming Guide)

It is not clear which file is getting error.
whatever the class name is giving error put cursor on that and press CTRL + DOT(.) it will suggest you to add the correct name space

Related

changing a label using a combobox and a button

hi im sorta new to doing projects myself but one of my first tasks was to make a btec grade to ucas points converter by allowing the user to select their grade from the combobox and it would show in a label the correlating grade
however i dont know how to get the selected combobox item to change the label for each different grade
i tried using a an if statement but i realised that it comes up with error CS0029
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 btec_to_ucas
{
public partial class Form1 : Form
{
string PPP = "48";
string MPP = "64";
string MMP = "80";
string MMM = "96";
string MMD = "112";
string DDM = "128";
string DDD = "144";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1 = PPP)
{
label1.Text = PPP;
}
}
}
}```
Problem is here: if (comboBox1 = PPP). In this case, comboBox1 is the whole combo box object, with its items, selected index, size, etc. That will never be equal to the string "48". You want to look at the value that has been entered. Try comboBox1.SelectedText.
See:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox?view=netcore-3.1

Dynamic html news page is not working in C#

I'm trying to create a dynamic news html page.
The problem occurs when I try to create the html dynamically. I'm new to C#, and don't know what's wrong. Here is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NewsAPI;
using NewsAPI.Models;
using NewsAPI.Constants;
namespace NewsDemo
{
public partial class Default1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
LoadNewsPage();
}
}
protected void LoadNewsPage()
{
try
{
var newsApiClient = new NewsApiClient("key");
List<string> mylist = new List<string>();
mylist.Add("google-news");
mylist.Add("bbc-news");
mylist.Add("cnn");
mylist.Add("the-new-york-times");
var articlesResponse = newsApiClient.GetTopHeadlines(new TopHeadlinesRequest
{
Sources = mylist,
Language = Languages.EN
});
if (articlesResponse.Status == Statuses.Ok)
{
string resulHtml = "";
resulHtml += "<table>";
foreach (var article in articlesResponse.Articles)
{
resulHtml += string.Format("<tr style='valign:top'>", "");
resulHtml += string.Format("<td width=20%><img src='{0}' width='250px' height='200px'></img></td>", article.UrlToImage);
resulHtml += string.Format("<td width=80%><a href='{0}'><h3>{1}</h3></a>{1}<br>{2}<br>{3}<br><br></td>", article.Url, article.Title, article.Author, article.Description);
resulHtml += string.Format("</tr>", "");
}
resulHtml += string.Format("</table>", "");
Label1.Text = resulHtml;
}
}
catch (Exception ex)
{
throw;
}
}
}
}
Whenever I try to run it, (in VS), Chrome opens and the page never loads. I have no idea what is wrong, as I have a console app that can fetch the news and that works fine.
Any help appreciated.
Thanks!
EDIT
Your comments to my answer helped my understand more.
I think your problem doesn't depend on your code but on a known problem with Chrome+Visual Studio
In short:
Either you can debug with another browser
Or you disable JavaScript debugging
I tested your LoadNewsPage() and it does get HTML just fine.
What seems strange is that you are inserting the HTML into the text of a Label.
Instead of doing that, add this HTML in your markup:
<div id="MyNewsSection" runat="server"></div>
And then replace this
Label1.Text = resulHtml;
with
MyNewsSection.InnerHtml = resulHtml;
Also, as a general debugging help, press F12 when in Chrome: if there are any errors, they will be shown to you.

Loading simples text file list on listview C#

so i was working with a text file data that contains a lot of simple lines and i want to put them on the list view exatly the same way as the listbox do. I need that because after i loading a long list on listbox, even it showing all my items, i cannot make a FindString() on it. i attached the comand to a combo box, and with other small lists it worked, but with this larger, seems that index reference doesn't work because of listbox limit.
So i was wondering if is possible to put, as example:
line1
line2
line3
line4
My text files hasn't this dots, i pu them just to make the example vertical.
On a list view. i used on listbox the method file.readllines to get it loading to it, and if exists a string find method to help me get the text on the lines. What should i do?
You can write the searcher you want by yourself.
It's very easy.
Just iterate on each data that exists in your ListView.
Then check your condition through an if-statement and do anything you want with the result!
Like this :
this.listView1.Items.Add("Test1");
this.listView1.Items.Add("Test2");
int Index = 0;
foreach (ListViewItem t in this.listView1.Items)
{
if (t.Text == "Test1")
Index = t.SelectedIndex;
break;
}
this.listView1.Items[Index].Selected = true;
I added couple of items to the ListView then iterate on it's items using a foreach, filter the items using an if-statement and finally show the item that I want.
You have to add an eventhandler. For example for the event of the ListView Load.
Then in the event handler you could load the contents of the file using the File class.
For example you could do it like this:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// ListView Loaded - Eventhandler
private void ListView_Loaded(object sender, RoutedEventArgs e)
{
string[] lines = File.ReadAllLines("E:\\test.txt");
foreach (string line in lines)
{
listview.Items.Add(line);
}
}
}
}
I have just tested the solution and it works fine.
I hope I got your intention.
thanks a lot of helping me, but i gandle it, i search a lot for extending the limit of listbox but no one was capable to help me out, but with your "foreach" code, this new way to search helped me to select my string on listbox, here's the code using a combo box to evertime updated get me the selected value from a gourgeous list:
declaring:
string result;
and then the combobox event:
private void comboBox1_TextUpdate(object sender, EventArgs e)
{
foreach (string item in listBox1.Items)
{
listBox1.SelectionMode = SelectionMode.One;
if (item == comboBox1.Text)
{
result = item;
}
}
listBox1.SelectedItem = result;
}

Combo Box and Entity Framework

I'm writing a simple first app using Winforms, C#, VS2010, and Entity Framework. Basically, I have a rich DB I'm tapping, and I've already set up the framework, successfully enough to populate a DataGridView control with a subset of the Work Order table.
Now, I want to place a combo box on the form ("cbProjectID") whose value is ProjectID and DisplayValue is ProjectNbr. I only want to put projects in the combo box list that are related to WorkOrders, and only unique ProjectIDs within that set (a project may have dozens of work orders....)
I'm assuming I need to generate a list from EF, using LINQ. I'm pretty new at LINQ, and I'm not figuring it out...Here's my code so far...
using System;
using CPASEntityFramework;
using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BrowseWorkOrders
{
public partial class BrowseWOs : Form
{
public BrowseWOs()
{
InitializeComponent();
}
private void BrowseWOs_Load(object sender, EventArgs e)
{
var context = new CPASEntities();
var query = context.tblWorkOrders.Where(c => c.ProjectID==8);
tblWorkOrderBindingSource.DataSource = query.ToList();
// Now, I want to load up the Combo Box with all the projects in the Work Order Table
}
}
}
I've been through the net trying to find a method I understand, but I'm failing. Perhaps someone can help me out. Here's my Datasource (I assume I should NOT use tblProject, but instead use the tblProject inside tblWorkOrder in order to get my subset...)
Any help and/or guidance would be appreciated.
Here's the code now...
namespace BrowseWorkOrders
{
public partial class BrowseWOs : Form
{
public BrowseWOs()
{
InitializeComponent();
}
private void BrowseWOs_Load(object sender, EventArgs e)
{
// Following loads up all Projects into the cbProjectID Combo Box
var context = new CPASEntities();
var PrID = context.qryProjectIDNbrDescs.ToList();
cbProjectID.DataSource = PrID;
cbProjectID.ValueMember = "ID";
cbProjectID.DisplayMember = "ProjectNbr";
}
private void cbProjectID_SelectedIndexChanged(object sender, EventArgs e)
{
var context = new CPASEntities();
var query = context.tblWorkOrders.Where(c => c.ProjectID == (int)cbProjectID.SelectedValue).ToList();
tblWorkOrderBindingSource.DataSource = query;
}
}
}
You need the tblProject on the top because the other is for a single WorkOrder only. However, you need to filter the list with those who have at least on WorkOrder:
var projects = context.tblProjects.Where(p => p.tblWorkOrders.Any()).ToArray();
cbProjectID.DataSource = projects;
cbProjectID.ValueMember = "ProjectID";
cbProjectID.DisplayMember = "ProjectNbr";

How do I get the scroll position from Microsoft Word

I want to position an image on the page the user is looking at, however I cannot find how to get the currently visible page/scroll in pixels.
Anybody know which object and property could give me that?
Are you trying to control Word from outside Word or is it an integrated control?
I think you want: Object oMissed = doc.Paragraphs[1].Range;
This code below is for an InlineShape, not Shape object. Shape object is for text-wrapping.
Code:
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;
using Word = Microsoft.Office.Interop.Word;
namespace WordAddIn3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Word.Application wdApp = Globals.ThisAddIn.Application;
Word.Document doc = wdApp.ActiveDocument;
string fileName = "c:\\testimage.jpg"; //the picture file to be inserted
Object oMissed = doc.Paragraphs[1].Range; //the position you want to insert
Object oLinkToFile = false; //default
Object oSaveWithDocument = true;//default
doc.InlineShapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);
}
}
}
Microsoft: HOWTO: How To Get 32-bit Scroll Position During Scroll Messages
Similarly, you may want to look at this SO question on How do I get the scroll position from Microsoft Execl -- which I just realized was asked by you..

Categories