|
I am developing a program using asp.net 2.0. I am using C# as the programming language.
In this project I am connected to a SQLServer database (SQLServer 2005). I have created a stored procedure in this database and want to call it from C#, pass it some parameters and receive from it a return value.
I am not clear as to how to call it, pass the parameters and then access the return value.
I would appreciate any help/advise/suggestions/etc on this.
Thanks, Vern
Vern
|
|
|
|
|
Use an SqlCommand object:
<br />
<br />
using System.Data;<br />
using System.Data.SqlClient;<br />
<br />
SqlConnection objConnection = null;<br />
SqlCommand objCommand = null;<br />
<br />
objConnection = new SqlConnection(YOUR_CONNECTION_STRING);<br />
<br />
objCommand = new SqlCommand();<br />
objCommand.CommandType = CommandType.StoredProcedure;<br />
objCommand.Connection = objConnection;<br />
objCommand.Connection.Open();<br />
<br />
objCommand.Parameters.Add("@MyParam", MyParamValue);<br />
<br />
objCommand.ExecuteNonQuery();<br />
<br />
if (objCommand.Connection != null)<br />
{<br />
objCommand.Connection.Close();<br />
objCommand.Connection.Dispose();<br />
}<br />
<br />
if (objCommand != null)<br />
{<br />
objCommand.Dispose();<br />
objCommand = null;<br />
}<br />
<br />
|
|
|
|
|
kryzchek:
Thanks for the code. This code and other code I got from this site was very helpful.
Thanks again, Vern
|
|
|
|
|
See using System.Data.SqlClient;
Here is some code to play with. If you are returning a query use ExecuteReader instead.
SqlConnection connection = new SqlConnection("someConnectionString");
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "stored procedure name");
SqlParameter parameterCustomerId = command.CreateParameter();
parameterCustomerId.DbType = System.Data.DbType.Int64;
parameterCustomerId.Direction = System.Data.ParameterDirection.Output;
command.ExecuteNonQuery();
I feel so silly, my code doesn't work!
parameterCustomerId.ParameterName = "@customerId";
command.Parameters.Add(parameterCustomerId);
needs to be added before the execute.
File Not Found
|
|
|
|
|
Ennis:
Thanks for the tips. This wsa very helpful.
Thanks again, Vern
|
|
|
|
|
I am trying to get a 6 or 8 digit date and convert it to a 10 digit mm/dd/ccyy.
The date entered is going to be in mmddyy or mm/dd/yy.
Can you help me on how we do it.
Ps: This is my first project in C#. So excuse me if this is a very basic question.
Thanks,Arun
|
|
|
|
|
A regex will do this, or the Substring method on the string class will allow you to pull out the pairs of numbers and add a "/" between them.
string date = "120199";
string formated = date.Substring(0,2) + "/" + date.Substring(2,2) + "/" + date.Substring(4);
Note, it's possible to give the substring one parameter, and it grabs the rest of the string.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
You should be able to use any of these: DateTime.Parse , DateTime.TryParse . There is also a DateTime.ParseExact method you could use if you can guarantee the formats.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Thanks. The DateTime.ParseExact really worked.
When I try this code i get a "System.Exception._COMPlusExceptionCode"
"-532459699" Error. Can you help me out on that one.
string textDate = textBox1.Text;
DateTime MyDate; //= DateTime.Now;
DateTime date;
int len = textDate.Length;
//DateTime MyDate = DateTime.Now;
try
{
if (len == 6)
{
MyDate = DateTime.ParseExact(textBox1.Text,"MM/dd/yyyy", null);
MessageBox.Show(textDate + MyDate);
textDate = MyDate.ToString("MM/dd/yyyy");
}
}
catch (Exception)
{
MessageBox.Show("Enter a valid Date in mmddyy Format");
}
Thanks,Arun
|
|
|
|
|
Sorry, I don't see anything wrong with the code. I was able to run it without any problems, other than to remove the if (len==6) statement so I could test it.
Do you get this error everytime the code runs or just with certain input?
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
This is with any date value that I type and happens every time.
I have the same issue even after removing if (len ==6) statement.
Thanks,Arun
|
|
|
|
|
I got it. My mistake was to try using the date "121203" and trying to use the
DateTime.ParseExact(txtEffDt.Text, "MM/dd/yyyy", null) instead of DateTime.ParseExact(txtEffDt.Text, "MMddyy", null).
Thanks all for the help.
Thanks,Arun
|
|
|
|
|
No problem. Glad you were able to figure out what was going on. If you know you will only be accepting a limited number of input formats you can pass a string array to the DateTime.ParseExact method and it will try to match against any of the formats in the array.
You also might want to look at the DateTime.TryParseExact method as well. The benefit to this is that it doesn't throw the exception and returns a bool value that indicates if the parse passed. The new date is returned as an out parameter.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
i want to start a video in a powerpoint slide how can i do?
i tried sending mouse click into the video, however it is not working (
mouse_event, sendinput, sendmessage, peekmessage... all of these functions don't work to send click into video
Please write what ever you know to solve my problem
|
|
|
|
|
i want to draw 25 circle shapes in my form
5 circle per row in 5 columns
and these circles have event on_click
ma_refay
|
|
|
|
|
There are round button classes on CP.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum ) tHERE
|
|
|
|
|
Hi,
I tried to develop a windows service in C#.Net 2.0 to move files from a directory to another directory but my problem is this, when the file is uploaded that directory, event firs up, even the file is not completely uploaded, and it gives me error, as file is still uploading, is there any way to solve this problem other than while loop to check that file is completed uploaded and is acceable now, please suggest.
Many Thanks,
Adnan Rafiq
muhammadadnanrafiq@gmail.com
|
|
|
|
|
Hi Adnan,
Are you using a FileSystemWatcher to monitor the upload directory?
If so, you have to make a do/while loop and try do access the file in a try/catch block inside the do/while loop. If you can access the file (it doesn't throw an exception of being current in use) you set a flag in order to exit from the loop. I think it should look something like this:
<br />
bool uploadCompleted = false;<br />
do<br />
{<br />
try<br />
{<br />
File.Open("yourFileName", FileMode.Open, FileAccess.Read).Close();<br />
uploadCompleted = true;<br />
}<br />
catch<br />
{<br />
}<br />
}<br />
while (uploadCompleted != true)<br />
I done it before and it worked. I couldn't find another approach so solve it (even made some posts in CP to find an answer). The problem is that the event is fired up even when the copy/move/creation is not complete, so you can't access the file yet. :/
Someone developed a customized code do solve this and wrote an article about this wrapper here in CP, but I thought it was too complicated and done the almost same thing I'm saying after all.
Hope it helps you...
CYA
|
|
|
|
|
Hi, Thanks for replying and understanding problem
Please tell me one thing, if i implement this solution of do while loop, and the uploading directory is getting files every second what will happen in that case, i think its performance will be stuck out, as other thread have to wait to finish out the while loop, or i mean wt will happened in that case. using filesystemwatcher the only .net component available to watch a directory..............
Please i hope you will understand what i m trying to say.
Many Thanks,
Adnan Rafiq
muhammadadnanrafiq@gmail.com
|
|
|
|
|
Sure, performance in this case will be stuck.
I think you can do a Thread.Sleep() inside the do loop, like 500ms. It should help a little. I don`t know another aproach.
Hope someone help you more than I did. :]
Cheers
|
|
|
|
|
Hello,
I would like to display small window with web page inside window of antoher web page.
is it possible? how to do this using HTML?
Thanks!
cyberjoe
|
|
|
|
|
|
|
Hello,
I'm new to programming and C#, so please bear with my ignorance!
I need to extract jpeg images and header data from a binary file.
The binary file is formatted with several fixed length fields containing information about the jpeg image, followed by the jpeg itself, followed by more header data, another jpeg, etc...
Using a FileStream and BinaryReader I am able to read and store the metadata, because I know the length of the fields, but I am stumped on how to read and store the jpeg bytes since they vary in size.
There is a fixed record delimiter between each header data/jpeg record, so I was thinking of using that to break apart the records. Once they are seperated and the header fields read, I could just assume the rest is the jpeg and store that. I'm not sure how to go about doing that though.
Any suggestions or demo code is greatly appreciated!
Thanks!
|
|
|
|
|
Hi,
assuming you are in control of the file format, this is what I suggest:
prefix each record with a byte indicating the record type, and terminate the file with
yet another record type (I suggest a zero byte here). So a file would look like this:
type0 record0 type1 record1 type2 record2 ... typeN recordN 0
Now each record could correspond to a C# struct, and that struct could contain
a Save(stream) method to append the struct as a record to the file stream (dont forget
the type byte!), and a Load(stream) method to create and populate a struct by reading
from the file stream (starting after the type byte).
Now loading the file stream would consist of a loop containing
- read the byte that tells the record type
- use a switch to call the right struct's Load method
- if end-code, close stream
If you cant follow the above scheme (e.g. because the file format has been fixed
and does not include a type byte), then you need to determine the type of the next record
by reading and analyzing some bytes, then rewind a bit (using
Seek method or Position property) and Load a record; repeat until done.
Hope this helps.
|
|
|
|