|
You can use whichever you want, but I always use Add because IDataParameterCollection doesn't include AddWithValue .
|
|
|
|
|
use AddWithValue!
beacuse use a strongly type dataset functionality
|
|
|
|
|
Hi,
Maby this is simple for you, but for me is not.
I have this code:
int rezultat = 0;
try
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
rezultat = (int)cmd.ExecuteScalar();
}
catch (Exception ex)
{
lblMesaje.Text = "Eroare: " + ex.Message.ToString();
}
finally
{
if (sqlconn.State != ConnectionState.Closed)
{
sqlconn.Close();
}
}
return rezultat;
Is just for inserting a new record in a table. Even if this throw an error "Specified cast is not valid." "rezultat=(int)cmd.ExecuteScalar();" - the code is executed and the row is inserted in the database, and the execution continues.
Why it continues?
Maby i don't understand the try catch finally yet
Thank you!
|
|
|
|
|
The whole idea of try...catch blocks is to do exactly what you have done: catch an error, report it (or log it, or fix it) and let the software continue without crashing.
In your case, the exception is being thrown because the result returned by SQL is not an integer, and cannot be cast to an integer (it may be a DBNull, or a string for example - either put a breakpoint on the ExecuteScalar line and look at the return, or include the return value in your message to actually fix the problem). But the insert has been done by that time, so the row is in the DB already.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Thank you for your message!
Beceause now i try to learn some C# programming (and write in english), your message help me to understand about Try catch.
Just to be sure that i have understand:
1. I have my btnSave code that look like this:
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
int x = 0;
x = SaveData();
if (x > 0)
{
lblMesaje.Text = " Your new ID is: " + x.ToString();
}
}
catch (Exception ex)
{
lblMesage.Text = "Some error occured: " + ex.Message;
}
}
2. Now here is my Insert function:
private int SaveData()
{
//SQL objects set..
int result= 0;
try
{
if (sqlconn.State != ConnectionState.Open)
{
sqlconn.Open();
}
result= Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
catch (Exception)
{
throw;
}
finally
{
if (sqlconn.State != ConnectionState.Closed)
{
sqlconn.Close();
}
}
}
Now i throw and exception in SaveData function and the message is displayed to the user. Is ok?
Thanks again!
|
|
|
|
|
Looks good!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
This is fine, although the
Claudiu Schiopu wrote: catch (Exception)
{
throw;
}
doesn't really do much. You can just have the
try and
finally blocks, and the code would effectively do the same thing.
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
How do i run powershell script from c# button
i want to run as example simple powershell command
for example : Get-WmiObject Win32_ComputerSystem | Select name, in textbox
|
|
|
|
|
You probably need to use the Process.Start() [^] method.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Hi Richard, Just thought you might like to know that there's an article on CP which does this in a better way than Process.Start - see my comment below for a link. When I last did it, the method described seemed to work better than Process.Start so might be worth knowing about if you ever need to do this.
|
|
|
|
|
Thanks, looks like a very useful link. I guess I should really have remembered
this as I have done a bit of reading about PowerShell in the past.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
|
right now i'm having a c# tcp server that collect the data from different sites and insert it in to database and a web application that show data. What i want is to bind them in to one single web application.if it is possible please give me some ideas.
|
|
|
|
|
1) Specify it.
2) Design it.
3) Code it.
4) Test it.
5) Depending on the results of (4), repeat from appropriate stage from (1) to (3) or release it.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Can I change the ReceivedBytesThreshold in a serial port ?
I have some packets that will be large, and some that will be small, and I want to be able to allocate more time when I'm doing the large ones
|
|
|
|
|
add some id in packet header and define the id according to the size of the packet like '0' for small packet '1' for large packet.First parse the header and then according to the id allocate time.
|
|
|
|
|
I'm not very good with regular expressions. Google isnt' turning up much, but I think it's because I am not asking correctly.
I have a data annotation with multiple things I'm looking for in the regular expression validation. I have two words that can be typed, or it can start with one letter, but it cannot end in a letter unless it's on of the two words.
I've been tinkering with just starting with 2 letters, or ending with 1 letter. I found that one alright:
@"^[a-zA-Z0-9]{1}[0-9]+\d"
but I need the opposite of that. If I were to use that for validation I need something that does not match that pattern. I thought ^() would reverse it, but it didn't.
Urgentz codez?
If it moves, compile it
|
|
|
|
|
Maybe try a (?! subexpression ) Zero-width negative lookahead assertion.
|
|
|
|
|
thanks for the help.
If it moves, compile it
|
|
|
|
|
@"(^([0-9]|[A-Za-z][0-9])[A-Za-z0-9]+\d$)|(SpecialWord)|(OtherSpecialWord)"
I always do that. I post a question and then realize I was being a tard. This is the/a solution for the data annotation.
If it moves, compile it
|
|
|
|
|
Hi,
Does anyone know the exact location of the C#.net ascii table or where exactly it is generated. Can someone please assist me a path or file name.
Thank You.
|
|
|
|
|
for(byte i=0;i<255;i++){
Console.WriteLine(((char)i));
}
|
|
|
|
|
Thanks, but I don't need the code. I need the location of where to find it. Sorry about the misunderstanding.
|
|
|
|
|
What are you talking about? A file that contains a list of ASCII values!?
/ravi
|
|
|
|
|
I really don't know how to make this any clearer. I am looking for the specific "location" of where the process of generating ascii values are done with the .net environment.
|
|
|
|