|
I haven't programmed in about 3 years and a programming friend recommended it to me.
Could you give me an example? The more specific the better...I'm not very bright.
|
|
|
|
|
An example of the call, or of proper n-tiered development ?
SqlCommand cmd = new SqlCommand("InsertTrack", Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@artistName", (artist == null) ? "" : artist);
cmd.Parameters.Add("@trackName", (track == null) ? "" : track);
cmd.Parameters.Add("@albumName", (album == null) ? "" : album);
cmd.Parameters.Add("@filePath", filePath);
cmd.Parameters.Add("@isVideo", isVideo ? 1 : 0);
cmd.Parameters.Add("@thumbPath", (thumbPath == null) ? "" : thumbPath);
cmd.Parameters.Add("@trackID", -1);
cmd.Parameters["@trackID"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
return (int) cmd.Parameters["@trackID"].Value;
That's a call out of the data layer of a video jukebox I wrote. It represents a complete static function in a class that has the Connection as a lazy initialised static member property.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Christian Graus wrote: cmd.Parameters.Add("@trackID", -1);
Why -1 versus SqDbType.Int?
I'll be trying your version of code in a minute...be wary..I'll probably have more questions.
|
|
|
|
|
That code is a little over complex, it's passing in some values based on booleans. However, the add method takes a name and a value, not a value type, from the look of it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Okay, question time!
I have my connection in my web.config file and have this declared in my code behind:
private string connectionString = ConfigurationSettings.AppSettings["connectionString"].ToString();
So I changed my code to:
private object btnAdd_Click(object sender, System.EventArgs e)<br />
{<br />
strPartNumberInputReference = txtSearchPart.Text;<br />
strPartTypeID = ddPartType.SelectedValue;<br />
SqlCommand cmd = new SqlCommand ("UpdatePart_Step_1_Test3", connectionString);<br />
cmd.CommandType = CommandType.StoredProcedure;<br />
cmd.Parameters.Add ("@PartNumber", strPartNumberInputReference);<br />
cmd.Parameters.Add ("@PartTypeValue", strPartTypeID);<br />
cmd.Parameters.Add("@ID",SqlDbType.Int);<br />
cmd.Parameters["@ID"].Direction = ParameterDirection.Output;<br />
cmd.ExecuteNonQuery();<br />
return(int) cmd.Parameters["@ID"].Value;<br />
}
Problem 1: I bolded "connectionString"--the compiler is unhappy and not sure how to resolve this. Geting "cannot convert string..." so I assume I need to change the declaration...but what to?
|
|
|
|
|
It's actually an instance of a connection, which itself is built from the string.
SQLConnection is the class, I think.
// return(int) cmd.Parameters["@ID"].Value;
The return statement will work here, it will box the int and return it as an object. Why is this event handler trying to return an object, where was it called from ? An event handler returns void.
I doubt you want to return a boxed int tho, do you ? My method returned int and was inside a data layer, called from my business layer.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
I'm leaving in 10 min, so if you get them through in time, I'll be happy to help, otherwise it'll be tomorrow morning.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
leckey wrote: SqlParameter param6 = new SqlParameter(@ID, SqlDbType.Int);
Was this just a copy/paste error? I'm pretty sure @ID needs to be in quotation marks.
Also, you should use SCOPE_IDENTITY()[^] instead of @@IDENTITY to get the ID of the row you just inserted. It's not strictly necessary, but it's a good habit to get into. More details in the remarks section on the MSDN site.
Jon Sagara
When I grow up, I'm changing my name to Joe Kickass!
My Site | My Blog | My Articles
-- modified at 17:17 Wednesday 12th July, 2006
|
|
|
|
|
Jon, you are the winner!!! That was my problem. I need to get this done but will probably later take Christian's advice.
As Steve the Pirate says in Dodgeball...
"Gargh!"
|
|
|
|
|
Yeah, I missed that, although when he said, from what you said, I thought it was blowing up elsewhere.
Yes, you definately need to read up on seperation of the presentation tier, although your code will work, it's a design issue.
http://www.15seconds.com/issue/011023.htm[^]
looks like an OK place to start with a quick overview.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Many thanks again for your help!
|
|
|
|
|
I am not sure if this can be done. I am writing a console application to ftp files. This will be called by a scheduling program. Is there a way to return a value if the transfer fails?
Lyman Ring
|
|
|
|
|
See if this works
static int Main(string[] args)
{
return 3;
}
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
Thanks, My next question would be how to check this value
|
|
|
|
|
Make a .cmd file and put this in it
<yourappname>
echo %ERRORLEVEL%
pause
then double click the .cmd file
"Just about every question you've asked over the last 3-4 days has been "urgent". Perhaps a little planning would be helpful?" Colin Angus Mackay in the C# forum
led mike
|
|
|
|
|
Thank you very much for your help.
|
|
|
|
|
I am new to C# and have a class project to work on. I am trying to create an Amortization calulator the display the payment results based on the Time period thay have to repay the loan. The calculation part of it is working fine but I can not get it to display the result. Can I please get some help with this? See code Below:
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Label6.Text = "Final Balance: " +
CalculateBalance(Convert.ToInt32(TextBox1.Text),
(Convert.ToDouble(TextBox2.Text) / 100),
Convert.ToInt32(TextBox3.Text),
Convert.ToInt16(DropDownList1.SelectedItem.Value)).ToString();
}
private string CalculateBalance(int Principal, double Rate, int Years, int Period)
{
double result;
double NumToBeRaised = (1 + Rate / Period);
result=Principal * System.Math.Pow(NumToBeRaised, (Years * Period));
return (result.ToString("C"));
}
private string AmortDisplay(double Principal, double results, int Years, int Period)
{
int AmortTime;
double AmortPayment;
double RemBalance;
string Output;
AmortTime = (Years * Period);
AmortPayment = (results / AmortTime);
RemBalance = -(Principal - AmortPayment);
TextBox4.Text = "Duration\t \tPayment\t \tRemaining Balance \n" + AmortTime + "\t\t" + Convert.ToString(AmortPayment) + "\t\t" + Convert.ToString(RemBalance) + "\n";
}
protected void Button2_Click(object sender, EventArgs e)
{
AmortDisplay(Convert.ToDouble(TextBox1.Text),
Convert.ToDouble(Label6.Text),
Convert.ToInt32(TextBox3.Text),
Convert.ToInt16(DropDownList1.SelectedItem.Value)).ToString();
}
</script>
ext="Principal ($)" Width="165px" BackColor="Transparent" Font-Size="Medium"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Style="position: relative" BackColor="Silver"></asp:TextBox><br />
<br />
<asp:Label ID="Label3" runat="server" Style="position: relative" Text="Rate (%)" Width="165px" BackColor="Transparent" Font-Size="Medium"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Style="position: relative" BackColor="Silver" TabIndex="1"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label4" runat="server" Style="position: relative" Text="Years:" Width="165px" BackColor="Transparent" Font-Size="Medium"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Style="position: relative" BackColor="Silver" TabIndex="2"></asp:TextBox><br />
<br />
<asp:Label ID="Label5" runat="server" Style="position: relative" Text="Compound Frequency:" Width="165px" BackColor="Transparent" Font-Size="Medium"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Style="position: relative" BackColor="Silver" TabIndex="3">
<asp:ListItem Value="1">Annually</asp:ListItem>
<asp:ListItem Value="4">Quarterly</asp:ListItem>
<asp:ListItem Value="12">Monthly</asp:ListItem>
<asp:ListItem Value="365">Daily</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList><br />
<br />
<asp:Button ID="Button1" runat="server" Style="left: 9px; position: relative; top: 10px"
Text="Calculate" Width="207px" OnClick="Button1_Click" BackColor="Silver" TabIndex="4" Font-Size="Medium" /><br />
<br />
<br />
<br />
<asp:Label ID="Label6" runat="server" Style="position: relative" Width="165px"></asp:Label> <br />
<asp:TextBox ID="TextBox4" runat="server" Height="345px" Style="position: relative"
Width="871px"></asp:TextBox>
Greg
The Belizean Dan Dada!!!
|
|
|
|
|
Of course it doesn't display any result. You don't have any code that displays the result.
---
b { font-weight: normal; }
|
|
|
|
|
Hello,
If I have a method of type
public void myMethod()
{
}
And I create a delegate void NewDelegate(void)
What is the actual difference between the following calls and how they are handled by .NET and the OS
NewDelegate del = NewDelegate(myMethod);
del.BeginInvoke(null, null);
and
Thread t = new Thread(new ThreadStart(myMethod));
t.Start();
I know the delegate will use a thread from the thread pool, but will the thread t as well? What is the actual effect of one versus the other.
|
|
|
|
|
Calling BeginInvoke on a delegate makes the method execute on a thread from the CLR's thread pool. You can't get a refernce to that thread, so you don't have fine-grained control over it.
:josh:
My WPF Blog[^]
|
|
|
|
|
So at the end it's only the reference issue? So if I launch some form on the thread, and then close the form. Does my thread (BeginInvoke or Thread.Start) returns to the thread pool?
Another thing, if I need my thread to be STA, I must create a new instane of the thread, and then set the ApartmentStyle. Meaning I can't use Delegate.BeginInvoke. Right?
|
|
|
|
|
zaboboa wrote: So at the end it's only the reference issue? So if I launch some form on the thread, and then close the form. Does my thread (BeginInvoke or Thread.Start) returns to the thread pool?
A background thread will keep running unless you kill it or the UI thread is killed (i.e. close the app).
zaboboa wrote: Another thing, if I need my thread to be STA, I must create a new instane of the thread, and then set the ApartmentStyle. Meaning I can't use Delegate.BeginInvoke. Right?
I don't know.
:josh:
My WPF Blog[^]
|
|
|
|
|
It isn't just a reference issue, see my other post for details. Also look at this page from MSDN: About Thread Pools[^]. There is a section at the bottom entitled "Best Practices" that sums things up nicely.
As regards your second question about apartment models, according to the following quote from another part of MSDN, you cannot use a thread pool thread in STA:
"Each thread uses the default stack size, runs at the default priority, and is in the multithreaded apartment."
Hope that helps!
Sincerely,
Alexander Wiseman
|
|
|
|
|
Hello,
First, as a general answer to your question about threading, take a look at this web-page[^]. It is long, but well worth the read.
Jon Skeet (the author of that document) speaks of the difference between thread pool threads and threads created with ThreadStart . The main difference is that thread pool threads are already created and waiting for a job to execute. This means that it is faster to use a thread from the thread pool to do a task. That does not mean it is better, by any means. Mr. Skeet recommends (and I think this is wise) that you use threads from the thread pool if you have a short, quick task that you would like to be multithreaded, but that you should never use thread pool threads to perform long operations. Using threads from the thread pool for long operations is a bad idea because other applications need access to the threads as well. Also, the Framework itself uses threads from the threads pool, so it is bad idea to tie them up for a long time. If the Framework needs a thread and they are all involved in long tasks, then bad things can happen to your application.
To sum up: if you have long tasks to be accomplished by other threads, use ThreadStart without a doubt. The extra time and resources it takes to create a new thread will not matter so much. If you have quick and easy tasks which need to be accomplished by other threads, use a thread pool thread.
Hope that helps!
Sincerely,
Alexander Wiseman
|
|
|
|
|
Thank you very much guys for all your replies.
It was very helpfull.
|
|
|
|