|
I use ADO.net with the OleDb provider for Jet/Ace
See also: http://connectionstrings.com/excel[^]
and then it's simple SQL:
create table Test (ID number,Name string)
insert into Test VALUES ('1','Alice')
insert into Test VALUES ('2','Bob')
SELECT * FROM Test
Etc.
|
|
|
|
|
Hello all,
I'm trying to download a file from a folder on the server but I don't know how to parse the path inside of the HREF attribute of the anchor tage. I'm trying to modify the following code to be used with HTML anchor tag:
protected void Page_PreRender(object sender, EventArgs e)
{
DataTable dt = new DataTable("INFO");
dt.Columns.Add("url", typeof(string));
string[] fileEntries = Directory.GetFiles(Server.MapPath("~/Documents/"));
dt.Rows.Add("~/Documents/" + Path.GetFileName(fileName));
grid.DataSource = dt;
grid.DataBind();
}
I wanted to do something like the following on my Page.aspx file:
<a href="~/Documents/" + Path.GetFileName(fileName)"> DOWNLOAD </a>
However, the above cannot be done. Please help me find and alternative, thanks for your help.
|
|
|
|
|
Could you please ask questions about ASP.NET in the ASP.NET forum. That's where you will find ASP.NET experts.
|
|
|
|
|
Hi, this thread represents a summary of what I have learned since starting the thread [^] on this forum: re use of dynamic to allow direct access to a 'value without the need for casting that 'value (currently an 'object) to its appropriate type.
In the code below I compare and contrast for three different types of .NET types, KeyValuePairs, Tuples, and List<dynamic> what using 'dynamic enables compared to using 'object in the constructor of these types.
I am particularly interested in the reactions of Martijn Kok and DaveyM69, who were kind enough to post interesting comments on the original thread: I believe, that what I demonstrate here does change the implications of one comment by Martijn Kok (as I understood that comment: and my understanding may well be in error).
Of course, comments would be welcome about use of 'dynamic in general, and the potential "overhead" of using a Type like List<dynamic> !
I would also be interested in knowing your opinion if there is enough material here for a "Tip/Trick."
thanks, Bill
// KeyValuePair declarations
private KeyValuePair<Type, object> Kvp1 = new KeyValuePair<Type, object> (typeof(int), 199);
private KeyValuePair<Type, dynamic> Kvp2 = new KeyValuePair<Type, dynamic>(typeof(int), 199);
// Tuple declarations
private Tuple<Type, object> twoTuple1 = new Tuple<Type, object>(typeof(int), 199);
private Tuple<Type, dynamic> twoTuple2 = new Tuple<Type, dynamic>(typeof(int), 199);
// List<dynamic> declaration
private List<dynamic> dyList = new List<dynamic>();
// method that demonstrates use of declared objects above
private void TestDynamicVsNonDynamic()
{
// using KeyValuePair
//int a = Kvp1.Value + 1; // will not compile
int b = Kvp2.Value + 1; // will compile, give expected result
// using Tuple
//int x = twoTuple1.Item2 + 1; // will not compile
int y = twoTuple2.Item2 + 1; // will compile, give expected result
// using List<dynamic>
dyList.Add(199); // will compile
dyList.Add(new TextBox{Text = "some text&"});
int z = dyList[0] + 1; // will compile, give expected result
dyList[1].Text += " ... blah, blah blah";
string s = dyList[1].Text; // will compile, give expected result
Type dyList1Type = dyList[1].GetType(); // returns expected Type: TextBox
}
"If you shoot at mimes, should you use a silencer ?" Stephen Wright
|
|
|
|
|
Hi Bill,
The results you show are exactly what I would expect. Using object , although the Type is readily available, to use it as that type (using the + operator in your example) requires a cast:
int a = ((int)(Kvp1.Value)) + 1;
dynamic does not have this limitation as Kvp1.Value in your example will be dynamically resolved to an int at runtime and therefore has all the functionality of that struct.
This is the primary (only?) advantage of dynamic over object - using object can require casts all over the place! What the overhead is of dynamic (if any) I don't know as I've never found the need to use it in my own code.
|
|
|
|
|
Thanks, DaveyM,
What I remain puzzled by is that if I use, for example, a KeyValuePair<Type,object>: like this:
private KeyValuePair<Type, object> kvpTO;
private void messWith_kvpTO()
{
kvpTO = new KeyValuePair<Type, object>(typeof(string), "some text");
Type workingType = kvpTO.Key;
// workingType now contains:
// [System.RuntimeType] = {Name = "String" FullName = "System.String"}
// fail "cannot recognize kvpTO"
//string kvpString = (workingType) kvpTO.Value;
// fail "cannot recognize kvpTO"
//string kvpString = kvpTO.Value as workingType;
} I cannot find any way to use the valid run-time Type to convert the "string" from an object to a string in a generic way: where the Type could any valid Type, and I do not know what that Type is at run-time.
Have any idea how to do that ?
It was my inability to use the 'Type in the 'KeyValuePair to do "casting" from object: that led me to using 'dynamic.
best, Bill
"If you shoot at mimes, should you use a silencer ?" Stephen Wright
|
|
|
|
|
As you're storing the Type and the object in one pair so it's guaranteed to be a valid cast, you could use Convert.ChangeType[^] to do this:
string kvpString = Convert.ChangeType(kvpTO.Value, kvpTO.Key);
|
|
|
|
|
Hi Dave,
string kvpString = Convert.ChangeType(kvpTO.Value, kvpTO.Key); I am afraid that one will not work: "Error Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)"
You'll note the examples on the MSDN page you linked to all use a specific cast on the result returned by calling Convert.ChangeType:
Double d = -2.345;
int i = (int)Convert.ChangeType(d, typeof(int));
Console.WriteLine(&amp;quot;The double value {0} when converted to an int becomes {1}&amp;quot;, d, i);
string s = &amp;quot;12/12/98&amp;quot;;
DateTime dt = (DateTime)Convert.ChangeType(s, typeof(DateTime));
"If you shoot at mimes, should you use a silencer ?" Stephen Wright
|
|
|
|
|
OK, looking at generics this is getting a bit 'hackish', but a few moments thought and this may work:
public static T GetValue<T>(KeyValuePair<Type, object> keyValuePair)
{
return (T)Convert.ChangeType(keyValuePair.Value, typeof(T));
}
Test:
KeyValuePair<Type, object> keyValuePair = MakeKeyValuePair(123.58);
int i = GetValue<int>(keyValuePair) + 1;
Console.WriteLine(i);
edit: forgot this method!:
public static KeyValuePair<Type, object> MakeKeyValuePair(object obj)
{
return new KeyValuePair<Type, object>(obj.GetType(), obj);
}
|
|
|
|
|
Hi DaveyM69,
Yes, this will work, but note that you are required to know what Type you want returned, and specify it in the call to GetValue<T> : to me that is really the same requirement as having to do an explicit cast on the result of Convert.ChangeType, just more elegant .
Appreciate your comments, but, so far, still convinced that 'dynamic adds something unique to this (albeit esoteric) area of .NET Type conversion we are exploring.
... edit ...
Note that this code, similar to yours in structure, would work without the need for type specification:
public static dynamic x_GetValue<T>(KeyValuePair<Type, object> keyValuePair)
{
return Convert.ChangeType(keyValuePair.Value, typeof(T));
}
string kvpString = x_GetValue<dynamic>(kvpTO);
var kvpString = x_GetValue<dynamic>(kvpTO);
kvpString += "... even more text";
Console.WriteLine(kvpString.GetType() + " : " + kvpString); However, I think a possible valid "challenge" to using 'dynamic, compared to other strategies that return 'object: is that the programmer is still going to need to know, in all cases, what type of value will be returned here ... unless the programmer implements some kind of switch/case statement to take action based on the run-time type.
I'm trying to imagine a real-world use case where using 'dynamic, in the ways explored on this thread, would be really required, and "best practice:" I admit to failing to do that ... so far.
Would it benefit others on CP, to write up this usage of 'dynamic as a tip-trick ? Not sure !
... end edit ...
best, Bill
"If you shoot at mimes, should you use a silencer ?" Stephen Wright
modified 29-Aug-12 0:21am.
|
|
|
|
|
BillWoodruff wrote: the programmer is still going to need to know, in all cases, what type of value will be returned
That is the issue. Regardless of object, dynamic, var or whatever, to use the return value for anything you're gonna need to know what it is.
BillWoodruff wrote: Would it benefit others on CP
Yes, if you can find a use case
|
|
|
|
|
in trying to make better use of the using statement i came across some syntax i haven't been able to sort out.
basic MailMessage syntax sets properties separated by commas, like this:
MailMessage message = new MailMessage()
{
From = new MailAddress("mailx@website.com", "it's me."),
Subject = subject,
Body = userMessage
};
...
message.Dispose();
if i add a using statement, i haven't found a way of setting those properties with the same syntax. i've had to do it "long-hand", with message.From, message.Subject, etc.:
using ( MailMessage message = new MailMessage() )
{
message.From = new MailAddress("mailx@website.com", "it's me.");
message.Subject = subject;
message.Body = userMessage;
...
mailClient.Send(message);
}
is there a way of setting the properties in a comma-separated way with a using statement?
|
|
|
|
|
using (Form f = new Form() { Text = "Hello World" })
{
f.ShowDialog(this);
}
Works for me.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Did you try
using(MailMessage message = new MailMessage()
{
From = new MailAddress("mailx@website.com", "it's me."),
Subject = "test",
Body = "Hello"
})
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
thanks. so i have this now:
using ( MailMessage message = new MailMessage()
{
From = new MailAddress("mailX@website.com", "it's me."),
Subject = subject,
Body = userMessage
})
{
message.To.Add(new MailAddress(_strMailAddressX, "FromX"));
message.To.Add(new MailAddress(_strMailAddressY, "FromY"));
SmtpClient mailClient = new SmtpClient();
mailClient.Send(message);
}
return true; all of that is wrapped in a try block. now,
- i could not get the 'To.Add' part to work without 'message.To.Add'. it wouldn't work at all inside the block that sets the other properties. i see that 'To' is a collection but i'm surprised i couldn't add to it while setting the other props.
- it kind of looks like the 'using' portion ends half-way through, when the right parenthesis closes. i tried other places but couldn't get anything else to work. at what point does it actually end here? is the 'using' stmt set up properly now?
- 'return true' is there because i'm setting this up in a class that'll return a bool. all comments appreciated.
tnx.
|
|
|
|
|
Hi,
I want to know how can I play incoming message notification using PC speaker?
can you please help...
|
|
|
|
|
http://msdn.microsoft.com/en-us/library/4y171b18.aspx[^]
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
what i want is to force playing sound notification even if the sound volume was set to zero, similar to the old days when sound is played on DOS
|
|
|
|
|
|
Console.Beep[^]. Unless it's a 64-bit[^] system.
The cable to the internal speaker is the first thing I yank out of every PC I work on. I imagine more people being annoyed enough by the sound to have done something similar.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Good catch. That thing is so annoying that it does not even cross my mind that someone would actually use it. I do not even know if that last 10 computers that I have had, the speaker even worked or not.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Thanks
|
|
|
|
|
I have MDI Parent form. I want an alawys visible MDI Child to act like a side bar with a fixed width and every new MDI child I open will occupy the remaining space only when I dock it to fill?
how can I do this?
|
|
|
|
|
jrahma wrote: how can I do this?
public class FormMyToolBox: Form
{
public FormMyToolBox (string aCaption, DockStyle aDockStyle)
{
Text = aCaption;
FormBorderStyle = FormBorderStyle.FixedToolWindow;
Dock = aDockStyle;
TopLevel = false;
Visible = true;
}
Controls.Add(new FormScriptToolBox(
"Ze ToolBox",
DockStyle.Right));
Works for me under Mono. Specifying the maximum or minimum width is done using the appropriate properties[^].
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
sorry this is duplicate post...
kindly delete...
thanks...
|
|
|
|
|