C# - Use String Value To Create New Instance?
May 6, 2010
I have a few classes: SomeClass1, SomeClass2.
How can I create a new instance of one of these classes by using the class name from a string?
Normally, I would do:
var someClass1 = new SomeClass1();
How can I create this instance from the following:
var className = "SomeClass1";
I am assuming I should use Type.GetType() or something
View 3 Replies
Similar Messages:
Dec 9, 2010
I have 3 Databases that I am pulling data from for my web site. 2 of the databases are in the same SQL instance, however the 3rd one is in a different SQL instance. Both of the SQL instances are installed on the same server, one of them is a Named instance and the other is installed under the default instance. So my connection strings look something like this:
<add name="ABCConnectionString" connectionString="Data Source=BobDataDB;Initial Catalog=Bob;User ID=*******;Password=*********;" ProviderName="System.Data.SqlClient"/>
<add name="BCDConnectionString" connectionString="Data Source=BobDataDB;Initial Catalog=Sue;User ID=*******;Password=*********;" ProviderName="System.Data.SqlClient"/>
<add name="EFGConnectionString" connectionString="Data Source=Bob;Initial Catalog=Sue;User ID=*******;Password=*********;" ProviderName="System.Data.SqlClient"/>
I'm in the process of moving this application from the Development Server to the Production Server when I noticed this issue. Previously the ABC and BCD connectionStrings where pointing to a different server and instance. So my question is how do I get this wo work with the server names being the same but the instances being different?
View 4 Replies
Sep 26, 2010
I have a question regading sql name instance ???
When ever i create new instance in sql server 2005 , it use to create the new instance along with my PC name.
For Eg :
My PC Name is Web and New instance name is Busiwork it use to create like WebBusiwork. But i need just the sql instance name of my server as Busiwork.
View 5 Replies
Aug 5, 2010
As some may be aware, I recently posted about high memory usage on my website and I have an idea that my thumbnailer may have something to do with this as I am not actively disposing of the instance when it has been used due to my misunderstanding how it works.
I am now looking at my code for the thumbnailer and would like some advice on when something would actually need disposing of, is it ONLY when you create a new instance of an object?
Like:
Target := System.Drawing.Bitmap.Create(Trunc(Width), Trunc(Height));
MyImage := Target.FromFile(PhotoPath);
So, my question would be, do I need to dispose of both Target and MyImage to make sure the GC does what it needs to do properly?
View 4 Replies
Apr 4, 2011
A instance of a class is created in the partial class of an aspx page.Under page_load or button click method I'm trying to set the value to the class. but when each postback takes place new instance is created and I'm losing the previous value.
public partial class DatabaseSelection : System.Web.UI.Page
{
DBProperties dbpro;
Metadata obmeta;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dbpro = new DBProperties();
}
View 4 Replies
May 21, 2010
I wanna do smth like this:
[Code]....
Does anyone have a recommendation how this is archievable? I am trying to pass in the Type to the method and then create an instace of it and then access the properties in it.
View 8 Replies
Aug 6, 2010
i'm a beginner web programmer. I've been coding desktop applications primarily. Right now, i have created this web app, in Silverlight, that uses a web service of my own for querying the database. The thing is, one of the app functionalities is the ability to open PDF files. I know that silverlight won't let you do this, but using an IFrame on top of the silverlight application you are able to display a page with de pdf file (using acrobat plug in). So here's the problem, my silverlight app passes the pdf path to the web service and, in return, the web service would create a new Page and pass the new page URI back so that it can be displayed on the IFrame:
[Code]....
View 1 Replies
Sep 24, 2010
Bit of a puzzler, I have a generic class
public abstract class MyClass<T> : UserControl
{
}
and I have got a type like this
Type type = Type.GetType("Type From DB as String", true, true);
and I want to create and instance of MyClass using the type... But this doesn't work.
MyClass<type> control = (MyClass<type>)LoadControl("/UsercControl.ascx");
View 4 Replies
Apr 16, 2010
I am trying to create instance of class by using reflection in ASP.net web site. Class ClassName is defined and located in App_code folder. Following line returns null, what could be wrong.
Type type = Type.GetType("NameSpace.ClassName", false, true);
View 5 Replies
Feb 16, 2011
I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success....[URL]
The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.
I added a reference to the control in the .aspx page that will use it, like this:
<%@ Reference Control="~/PictureGallery.ascx" %>
And in the UserControl I added a ClassName like this:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>
When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".
The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck. So, I'm not able to get a reference to the UserControl.
View 3 Replies
Oct 12, 2010
In my controller method, I'm trying to create an instance of a View (cshtml file) before I wrap it around an ActionResult and return it.Since there is no "class" for Razor Views to speak off, how does one go about creating an instance of a Razor View?
View 25 Replies
Oct 20, 2010
How can I create an instance of a web control at runtime using reflection? I created a series of controls that implement a common interface and I would like to create these controls based on the name of the control which is stored in my database.
I have attempted (and failed) to create an instance of these controls using Activator.CreateInstance in the following ways:
Activator.CreateInstance("MyUserControl")
and
Activator.CreateInstance("ASP","controls_myusercontrol_ascx")
...and both return null.
I've also attempted to simply get the type of the control by trying..
Type t = Type.GetType("controls_myusercontrol_ascx");
and
Type t = Type.GetType("MyUserControl");
...and it returns null.
If I explicitly declare an object as controls_myusercontrol_ascx or MyUserControl, there is no issue -- but it can't be found with reflection.
Is it possible to create web user controls using reflection at run time? If so, how can I?
View 1 Replies
Mar 31, 2010
I am trying to compile the following code and i am getting the error:
Cannot create instance of abstract class .
m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));
// Create an array for the headers and add it to cells A1:C1.
object[] objHeaders = {"Order ID", "Amount", "Tax"};
m_objRange = m_objSheet.get_Range("A1", "C1");
m_objRange.Value = objHeaders;
m_objFont = m_objRange.Font;
m_objFont.Bold=true;
// Create an array with 3 columns and 100 rows and add it to
// the worksheet starting at cell A2.
object[,] objData = new Object[100,3];
Random rdm = new Random((int)DateTime.Now.Ticks);
double nOrderAmt, nTax;
for(int r=0;r<100;r++)
{
objData[r,0] = "ORD" + r.ToString("0000");
nOrderAmt = rdm.Next(1000);
objData[r,1] = nOrderAmt.ToString("c");
nTax = nOrderAmt*0.07;
objData[r,2] = nTax.ToString("c");
}
m_objRange = m_objSheet.get_Range("A2", m_objOpt);
m_objRange = m_objRange.get_Resize(100,3);
m_objRange.Value = objData;
// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book2.xls", m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();
View 2 Replies
Jan 14, 2011
I have a function that at the moment takes a Type variable. This function sticks it in a list and such and eventually needs will create a class of that type. Right now I do that with
object o=MyType.GetConstructors()[0].Invoke(new object[0]);
which is pretty hacky and also will not work in medium trust due to reflection(I think). Is there a better way of doing this without reflection? The Type is defined this way as part of a function. I need the class to be created "lazily" because it may not be created in the application if it's not needed. I use it for example like
AddToList(typeof(Whatever));
Note, I'm open to suggestions on changing the function calling. I just need the object to be created lazily and for to store the type(or however to create an object of the type) in a list.
View 3 Replies
Jan 11, 2010
I get following error: Target string size is too small to represent the XML instance
When I do this: repeater.DataBind();
How can I avoid this?
View 1 Replies
Feb 6, 2010
I want to create multiinstance application in Azure. and then demontrate that if one of the instance is down other instance is working fine. How should I achive that.
View 1 Replies
Jul 20, 2010
I'm trying to use MS IME on a server to retrieve some Japanese info in a silverlight app. The app accesses to the server by using WCF, but when calling a CoCreateInstance to create a IFELanguage2, it gives me -2147467262. The same code I'm using on the server actually works pretty fine on a WPF app.
Here is the code,
// ...omit
Guid imeGuid;
int errCode = Ole32.CLSIDFromString("MSIME.Japan", out imeGuid);
WinBase.CheckError(errCode);
Guid feLangIID = new Guid(Constants.IID_IFELanguage2);
IntPtr ppv;
errCode = Ole32.CoCreateInstance(imeGuid, IntPtr.Zero, Ole32.CLSCTX.CLSCTX_ALL, feLangIID, out ppv);
WinBase.CheckError(errCode); // errCode is 2147467262
IFELanguage IfeLanguage = Marshal.GetTypedObjectForIUnknown(ppv, typeof(IFELanguage)) as IFELanguage;
errCode = IfeLanguage.Open();
WinBase.CheckError(errCode);
// ...omit
Do I need to setup anything to use the MS IME on a server side? I'm running the app on the following environment, IIS7 .NET Framework 4.0 Windows 7 pro 64 bit VS2010
View 1 Replies
Dec 12, 2010
Error 1279 Cannot create an instance of the abstract class or interface 'System.Web.Mvc.FileResult'
[Code]....
I am using MVC 2. The same code works in my onather application. I have no idea about this error.
View 2 Replies
Sep 18, 2012
I am trying to get dropdownlist value in cs page but getting error "Use the new keyword to create object instance".
DropDownList tn = (DropDownList)Page.FindControl("DropDownListTRAIN_NO");
string t1 = tn.SelectedValue.ToString();
View 1 Replies
Jul 11, 2010
I am trying to create an instance of System.Web.HttpResponse to send the uers to another website.I am doing some other stuff in my vb.net that does not let me use the regular Response.Redirect.when I do Dim myRsp As System.Web.HttpResponse = New System.Web.HttpResponseit complains about a textwriter.
View 4 Replies
Dec 28, 2010
In my GridView I use FindControl/<FooterTemplate> with a TextBox that you can enter an integer with a comma
into the TextBox, because I create an instance of the Textbox first and then remove the comma out of the integer before the TextBox is used to update the table in the database.
However once the data is in the table and displayed in the GridView if you try to change the integer and use a comma in the GridView you cannot because FindControl/<EditItemTemplate> will not create an instance for you to remove the comma from the integer before it updates the table because FindControl/<EditItemTemplate> doesn't allow you to create an instance of the TextBox first, instead when you try to use the instance that looks like was created it gives an error stating:
Object reference not set to an instance of an object.
So the question is:
HOw do you create an instance of a TextBox in a GridView when using FindControl/<EditItemTemplate>?
<form id="form1" runat="server">
<div>
</div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
[Code]....
View 2 Replies
May 7, 2015
I am getting object refernce error at line:if (Session["Tax"].ToString() == "9")
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Tax"].ToString() == "9")
{
lblTax.Text = Session["Tax"].ToString();
}
else
{
lblTax.Text = "0";
}
}
Is anything wrong I have done?
View 1 Replies
Jan 9, 2011
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server" >
Dim connStr As String = ConfigurationManager.ConnectionStrings("cc").ConnectionString.ToString
dim db As New Data.SqlClient.SqlConnection(connStr)
why this error
Object reference not set to an instance of an object.
View 2 Replies
Feb 6, 2010
vs asp2008 instance cant recognize the sqlExpress 2008 instance on my machine
I have wonder if you could try to help me with the fallowing issue:When trying to add a new sql server connection (local or remount) from VS(visual studio) 2008 express - on the add connection dialog
box: server name: are Empty!!
The vs couldn't point to any database, like dont recognize the sqlExpress 2008 instance i got on my machine.
1. Have tried to "play" with sa property's(from sqlS: Databases: security: login tree folder) and with the sql
s windows service: stop and restart
2. And so with the target db.
3.
On the services.msc
The sql server express the status is started.
The sql server agent status is disabled and don't have the option to start.
The sql server explorer status is disabled and don't have the option to start.
4. And so reinstalled the asp.
View 2 Replies
Nov 16, 2010
I have a datamodel with a couple of tables. One of my tables have about 40 fields (I know that is a lot, but we have to keep the structure in place as we are upgrading a classic ASP project to MVC). Some of my Actions only updates 1 or 2 fields in my table.
Is there a way to create Entities, for instance, that only contains the contact details of the client table, and not any other details, and then another entity that contains only the address details. I don't want to submit the entire row when I only update telephone details, or the client's picture.
View 1 Replies