|
Oh ok, well then it should work, shouldn't it? Unless that code is broken and no one ever noticed, which is, of course, possible.
Are you sure you're expecting the right thing?
|
|
|
|
|
I have a C# object, something like:
class GUIItem
{
string Name;
string Value;
..
.. some random GUI properties
..
}
have another one that represents a database table:
class SomeDBObject
{
string Name;
string Value;
..
.. some random DB properties
..
}
So what happens is that the GUI builds up the GUIItem objects from an XML resource. However, it only has the Name property. It doesn't have the Value since that is stored in the DB. The concept is that the XML resource tells the GUI how to build the UI and the DB stores the values.
So to load the UI, I need to restore the Value properties from the DB.
So what I need to do is to match up the GUIItem and the SomeDBObject by the Name property and copy SomeDBObject.Value over to GUIItem.Value.
I know I can do this easily with a nested for loop O(n^2). But is there some cooler algorithm?
I think most of the time the KVPs in the database and the UI are going to be in the same order, so I can get by with an O(n)... but its possible a new item might be added to the GUI that is not yet configured in the DB and that would throw things off. Or if an item in the GUI was deleted or re-ordered for some reason.
Something in LINQ perhaps with some kind of join?
|
|
|
|
|
If you are certain that there is 1 DB entry per 1 GUIItem then you can build a HashTable[^] using the name as the key, then after you've initialized your GUIObjects and you are setting the values, the operation becomes an O(n), since element access in a hash table is O(n).
You'll still have to build the table, so really the operation becomes O(n) + O(n), once for building the table, and once when you are setting the GUIItems.
|
|
|
|
|
I'm curious, what benefit do you get from storing your gui item in xml rather than the database!
I have no answer but there must be a linq statement that does a join on the name fields in the 2 collections.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Well, it looks good idea I think but I've never seen like your idea yet.Sorry. 
|
|
|
|
|
i have a script manager at my master page, but i have 1 page require a scriptmanager because of cant page 2 scriptmanager hwo should i do? if i open a new webform without attach master page the function can work well?
Quote: <asp:toolkitscriptmanager id="ToolkitScriptManager1" runat="server" enablehistory="true" enablesecurehistorystate="false" scriptmode="Release">
|
|
|
|
|
I think this would be better in the ASP.NET forum.
Veni, vidi, abiit domum
|
|
|
|
|
script manager only can place on one page, since all the pages i attach with master page, so it will occur error, how should i should the problem that one of the page required script manager?
|
|
|
|
|
Did you read my previous message?
Veni, vidi, abiit domum
|
|
|
|
|
inside my database have binary data. what is this problem mean
Exception Details: System.IndexOutOfRangeException: ProductImage
Source Error:
Line 70: byte[] bytes = (byte[])sqlDataReader1["ProductImage"];
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn1.Open();
String sqlTitle1 = "SELECT * FROM ProductImage WHERE ProductID =@ID";
SqlCommand comm1 = new SqlCommand(sqlTitle1, conn1);
comm1.Parameters.AddWithValue("@ID", Id);
SqlDataReader sqlDataReader1 = comm1.ExecuteReader();
while (sqlDataReader1.Read())
{
byte[] bytes = (byte[])sqlDataReader1["ProductImage"];
string Imageitem = Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.ImageUrl = "data:image/png;base64," + Imageitem;
//Image1.Visible = true;
}
conn1.Close();
my product image table
ProductID datatype nvarchar(50) primarykey
ProductColorType nvarchar(50) primarykey
ProductImg varbinary(MAX)
thx for helping 
|
|
|
|
|
byte[] bytes = (byte[])sqlDataReader1["ProductImage"];
ProductImg varbinary(MAX)
Do you see a mismatch between these two names?
Veni, vidi, abiit domum
|
|
|
|
|
haha thx ^^ i copy from old project 
|
|
|
|
|
Always a good idea to try a bit of diagnosis before posting here.
Veni, vidi, abiit domum
|
|
|
|
|
|
I'm trying to find the way how to execute a string contain c# code.
Example
string st = "if (textBox1.Text == "Jon" && chkText.Checked)
MessageBox.Show("Yes!");
else
MessageBox.Show(textBox1.Text);";
I want to execute st then i want to see the result in a message box.
Please kindly help me. Thank in advance!
|
|
|
|
|
http://support.microsoft.com/kb/304655[^]
I'd recommend against putting the MessageBox and references in your textbox there; a simple check whether the text passed as an argument would suffice. The advantage being that there's no reference from the compiled code to your UI.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thanks but i get error.
Can u give me a small example!
|
|
|
|
|
jojoba2011 wrote: thanks but i get error. Can u give me a small example! If you'd get an error, it'd be more helpfull to solve that, than blindly trying examples.
I'm guessing that it's due to the fact that you only have the body of a method in there; just like in Visual Studio, the code needs to be located in a class. Try the example below in a console-application.
using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.CSharp;
class Program
{
static void Main(string[] args)
{
string sources = @"
using System;
public static class ExampleClass
{
public static void ExampleMethod()
{
Console.WriteLine(""Hello World!"");
}
}";
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
string Output = "Out.dll";
CompilerParameters parameters = new CompilerParameters()
{
GenerateExecutable = false,
OutputAssembly = Output
};
CompilerResults results = codeProvider.CompileAssemblyFromSource(
parameters, sources);
if (results.Errors.Count > 0)
{
foreach (CompilerError CompErr in results.Errors)
{
Console.WriteLine(
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";");
}
}
else
{
Assembly myAssembly = Assembly.Load(File.ReadAllBytes("Out.dll"));
Type myType = myAssembly.GetType("ExampleClass");
MethodInfo mi = myType.GetMethod("ExampleMethod");
mi.Invoke(null, null);
}
Console.WriteLine("Hit any user to continue");
Console.ReadKey();
}
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
vote => up.
That's a very useful, and complete, example, thanks !
"What Turing gave us for the first time (and without Turing you just couldn't do any of this) is he gave us a way of thinking about and taking seriously and thinking in a disciplined way about phenomena that have, as I like to say, trillions of moving parts.
Until the late 20th century, nobody knew how to take seriously a machine with a trillion moving parts. It's just mind-boggling." Daniel C. Dennett
|
|
|
|
|
thanks in advanced!
My question is this :
Is it possible to excute code without creating any class ....?
string sources = @"
using System;
public static class ExampleClass
{
public static void ExampleMethod()
{
Console.WriteLine(""Hello World!"");
}
}";
and one more :
How to use the textBox1.Text and other controls values?
|
|
|
|
|
jojoba2011 wrote: Is it possible to excute code without creating any class ....? No. Then again, it wouldn't be much work to wrap it automatically in a class; it's just text after all. Also keep in mind that this class will not be "unloaded".
jojoba2011 wrote: How to use the textBox1.Text and other controls values? You see that using? The code one compiles needs to have a reference to your project. That means including a "using" directive in the source code, as well as adding a reference[^] to the compilerparameters.
You could save a lot of pain by rewriting that part of the code to use variables/parameters, and not directly the UI-elements of your project.
--edit;
Alternatively, if you'd install Mono, there'd be the Eval[^] method.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
modified 16-Nov-13 16:18pm.
|
|
|
|
|
When I'm debugging, I do the following
string st = something
if (textBox1.Text == "Jon" && chkText.Checked)
MessageBox.Show(st + " (if)");
else
MessageBox.Show(st + " (else)");"
|
|
|
|
|
|
i got the solution :
http://stackoverflow.com/questions/14485226/convert-string-to-executable-c-sharp-code-in-code-behind
Solved !
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 Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private System.Windows.Forms.TextBox txtf;
private System.Windows.Forms.TextBox txts;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txt;
//public Form1()
//{
// InitializeComponent();
//}
public Form1()
{
this.txtf = new System.Windows.Forms.TextBox();
this.txts = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.txt = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtf
//
this.txtf.Location = new System.Drawing.Point(81, 125);
this.txtf.Name = "txtf";
this.txtf.Size = new System.Drawing.Size(100, 20);
this.txtf.TabIndex = 0;
//
// txts
//
this.txts.Location = new System.Drawing.Point(81, 161);
this.txts.Name = "txts";
this.txts.Size = new System.Drawing.Size(100, 20);
this.txts.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(81, 196);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txt
//
this.txt.Location = new System.Drawing.Point(12, 12);
this.txt.Multiline = true;
this.txt.Name = "txt";
this.txt.Size = new System.Drawing.Size(260, 107);
this.txt.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.button1);
this.Controls.Add(this.txts);
this.Controls.Add(this.txt);
this.Controls.Add(this.txtf);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private static string CreateExecuteMethodTemplate(string content)
{
var builder = new StringBuilder();
builder.Append("using System;");
builder.Append("\r\nnamespace Lab");
builder.Append("\r\n{");
builder.Append("\r\npublic sealed class Cal");
builder.Append("\r\n{");
builder.Append("\r\npublic static object Execute()");
builder.Append("\r\n{");
//builder.AppendFormat("\r\nreturn {0};", content);
builder.AppendFormat("\r\n {0} ", content);
builder.Append("\r\n}");
builder.Append("\r\n}");
builder.Append("\r\n}");
return builder.ToString();
}
private static object Execute(string content)
{
var codeProvider = new CSharpCodeProvider();
var compilerParameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
compilerParameters.ReferencedAssemblies.Add("system.dll");
string sourceCode = CreateExecuteMethodTemplate(content);
CompilerResults compilerResults = codeProvider.CompileAssemblyFromSource(compilerParameters, sourceCode);
Assembly assembly = compilerResults.CompiledAssembly;
Type type = assembly.GetType("Lab.Cal");
MethodInfo methodInfo = type.GetMethod("Execute");
return methodInfo.Invoke(null, null);
}
private void button1_Click(object sender, EventArgs e)
{
string Matn = txt.Text;
Matn = Matn.Replace(txtf.Name, txtf.Text);
Matn = Matn.Replace(txts.Name, txts.Text);
var result = new object();
result = Execute(Matn);
MessageBox.Show(result.ToString());
//result =Execute("DateTime.Now");
//result = Execute("if(\"ali\"==\"ali\")return \"true\"; else return \"false\";");
}
}
}
|
|
|
|
|
I've been trying to run a simple block of code from C sharp in a powershell run space.
I went to run that command in the powershell example project on my workstation and it did not work, but that demo application will run on the server that has scom installed.
Is there a management pack (like with Exchange) that I need to install on my workstation to get access to the dlls and commands that I have through powershell when on the server locally. Any ideas?
Import-Module –Name OperationsManager
New-SCOMManagementGroupConnection -ComputerName "serverName"
get-scomgroup
foreach ($group in get-scomgroup)
{
Write-Host $group
}
How to run PowerShell scripts from C#[^]
|
|
|
|