|
Steve Embry wrote: A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else
Looks like that variable is being defined somewhere else in the code; in other words you defined a variable with that exact same spelling and then tried to define it again somewhere else in the code. Try the previous suggestion of defining "strbody" before the statement and use the Find function (if you're using VS, navigate to Edit > Find and Replace > Quick Find) to try to locate all instances of that variable name, and see if maybe you used it for a different variable. Then just change the name (but, if you do, remember to update all references to the changed variable).
=======================
Every experience in life is a lesson to be learned
A. Stevens
B.S., Computer Science
|
|
|
|
|
While you have the answer to your question there is other points that I feel needs making.
I shudder with horror whenever I see a test against string content, I automatically assume the string can be edited by a user and you are screwed.
Next is the question of expanding criteria - what are you going to do when they add another term? And you know they will.
Built a construct such as a key word table when flags to define the function where it applies. The you can build a function that performs the test and return a boolean. This can be used in multiple places if required.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello-
I'm fairly new to C# (classic ASP guy), and I'm having a heckuva time with this. I am hoping someone can point out my flawed logic.
I first need to ascertain if the current article group is one of several that need to be treated differently. If it is, it defines the var "strbody" using a complete value as retrieved from the database. If it is not one of those special article groups, then I have to do some string manipulation to format the retrieved value before defining and displaying it.
My string manipulation code is flawless, but neither of the strbody vars I define in my if/then/else block is recognized when I call it below the code block???
@{
string group = Model.ArticleGroupName;
if (group.Contains("Spacial Orientation")||group.Contains("Topography")||group.Contains("Osteology")||group.Contains("Angiology")||group.Contains("Neurology")||group.Contains("Myology")||group.Contains("Radiology")||group.Contains("Misc. Drawings")||group.Contains("Clinical Testing"))
{
var strbody = item.ShortBody;
}
else
{
string s = item.ShortBody;
string sLess = s.Remove(0, 12);
int index = sLess.IndexOf("Summary");
var strbody = (sLess.Substring(index + 8));
}
}
@strbody
That code results in the following error:
\Plugins\FoxNetSoft.Articles\Views\ArticleRead\List.cshtml(76): error CS0103: The name 'strbody' does not exist in the current context
I am new to this, so please don't hesitate to chastise me for doing dumb stuff...I need to learn!
Thanks,
Steve
|
|
|
|
|
"strbody" needs to be declared outside of the if statement, so that you can access it outside the scope of the if statement.
@{
string group = Model.ArticleGroupName;
string strbody = string.Empty;
if (group.Contains("Spacial Orientation") || group.Contains("Topography") || group.Contains("Osteology") || group.Contains("Angiology") || group.Contains("Neurology") || group.Contains("Myology") || group.Contains("Radiology") || group.Contains("Misc. Drawings") || group.Contains("Clinical Testing"))
{
strbody = item.ShortBody;
}
else
{
string s = item.ShortBody;
string sLess = s.Remove(0, 12);
int index = sLess.IndexOf("Summary");
strbody = (sLess.Substring(index + 8));
}
}
@strbody
|
|
|
|
|
Thanks for the response!
I tried declaring the strbody as you suggested previously, but when I did, I got this error:
A local variable named 'strbody' cannot be declared in this scope because it would give a different meaning to 'strbody', which is already used in a 'parent or current' scope to denote something else
Learning C# is really frustrating! I could have done this exact thing with VBScript in about 2 minutes..and 8 less lines of code...
|
|
|
|
|
Inside your if statement where it says "var strbody = ..." should now only be "strbody = ..." since you already declared the variable outside of the if statement. If you look at the modified code I provided in my previous post you will see that I am just assigning a value to the strbody variable inside of the if/else statement.
|
|
|
|
|
I spotted that moments after submitting my reply...
It's working perfectly now...thanks man!
|
|
|
|
|
Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C#. Also, is there any real advantage to stored procedures vs parameterized queries?
For Example:
using (SqlConnection dbConnection = new SqlConnection(GSettings.SQLConnectionString))
{
dbConnection.Open();
for (int i = 0; i < DBUpdateList.Count; i++)
{
string selectSQL = "BEGIN TRANSACTION; UPDATE TABLE SET Dog = <a href="/Members/dog">@Dog</a>; COMMIT;";
using (SqlCommand dbCommand = new SqlCommand(selectSQL, dbConnection))
{
dbCommand.Parameters.AddWithValue("<a href="/Members/dog">@Dog</a>", "Poodle");
dbCommand.ExecuteNonQuery();
}
}
}
|
|
|
|
|
Personally I would only use BEGIN TRANSACTION; END in a stored procedure.
If you want to use transactions with your ADO.NET query I would read this MSDN transactionscope[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
BBatts wrote: Is there any benefit
No.
BBatts wrote: stored procedures vs parameterized queries
Those aren't mutually exclusive; even if you use stored procedures, you should still use a parameterized command to provide values (as necessary).
I also recommend that you not keep reinstantiating the Command, but reuse one:
instantiate command
add parameter(s)
(begin transaction if desired)
loop:
set Value(s)
Execute
(commit transaction)
|
|
|
|
|
BBatts wrote: <layer>Is there any benefit to adding BEGIN TRANSACTION; and COMMIT; to the beginning and end of a parameterized query in C# I'd recommend getting a SQL manual, and that's not meant as a snarky remark; programming often involves databases, and you'll need to dig into transactions sooner or later. Life also becomes easier when you can look up what each statement does
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Got one you would recommended?
|
|
|
|
|
Hi,
Did anyone tried Xamarin to develop iOS or Android apps using C#?
How was it?
Technology News @ www.JassimRahma.com
|
|
|
|
|
That's not a specific question.
Yep, I have seen that ThatsAlok[^] recently posted more than bunch of articles[^] on that topic. You could find few more CP members here.
And I suggest you to take a look at Mobile Development[^] section @ Codeproject.
|
|
|
|
|
private void Update_Click(object sender, EventArgs e)
{
try
{
if (connect.State == ConnectionState.Open)
{
connect.Close();
}
connect.Open();
SqlCommand cmd = connect.CreateCommand();
cmd = new SqlCommand("sp_UpdateDepartStaff", connect);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@StaffID", SqlDbType.Int).Value = logInIDTextBox.Text.ToString();
cmd.Parameters.AddWithValue("@LogInID", SqlDbType.Int).Value = staffIDTextBox.Text.ToString();
cmd.Parameters.AddWithValue("@StaffRoleID", SqlDbType.Int).Value = staffRoleIDTextBox.Text.ToString();
cmd.Parameters.AddWithValue("@FirstName", SqlDbType.NVarChar).Value = firstNameTextBox.Text;
cmd.Parameters.AddWithValue("@LastName", SqlDbType.NVarChar).Value = lastNameTextBox.Text;
cmd.Parameters.AddWithValue("@UserName", SqlDbType.NVarChar).Value = userNameTextBox.Text;
cmd.Parameters.AddWithValue("@Password", SqlDbType.NVarChar).Value = passwordTextBox.Text;
cmd.Parameters.AddWithValue("@Email", SqlDbType.NVarChar).Value = emailTextBox.Text;
cmd.Parameters.AddWithValue("@Phone", SqlDbType.NVarChar).Value = phoneTextBox.Text;
cmd.Parameters.AddWithValue("@StaffRole", SqlDbType.NVarChar).Value = staffRoleTextBox.Text;
cmd.Parameters.AddWithValue("@Department", SqlDbType.NVarChar).Value = departmentTextBox.Text;
// cmd.Parameters.Add(new SqlParameter("@ProfilePic", SqlDbType.Image));
MemoryStream MemStream = new MemoryStream();
byte[] DataPic_Update = null;
this.profilePicPictureBox.Image.Save(MemStream, ImageFormat.Png);
DataPic_Update = MemStream.GetBuffer();
MemStream.Read(DataPic_Update, 0, DataPic_Update.Length);
// image content
SqlParameter photo = new SqlParameter("@profilePic", SqlDbType.Image);
photo.Value = DataPic_Update;
cmd.Parameters.Add(photo);
int temp = 0;
temp = cmd.ExecuteNonQuery();
if (temp > 0)
{
connect.Close();
MessageBox.Show("Your info updated successfully... ");//, MessageBox.Show.Information, "Record Updated");
}
else
{
connect.Close();
MessageBox.Show("Your info modification failed ");//, MsgBoxStyle.Critical, "Modificatio Failed");
return;
}
//cmd.ExecuteNonQuery();
//MessageBox.Show("Updated Successfully!");
//connect.Close();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}
|
|
|
|
|
What value is returned in temp?
Do you have the stored procedure code?
A message that something didn't work is not very helpful - you need to debug to find any return messages or error codes
=========================================================
I'm an optoholic - my glass is always half full of vodka.
=========================================================
|
|
|
|
|
1. Your first 3 parameters are defined as integer but you are passing a string.
2. Why are you converting a Textbox.Text to string? e.g Textbox.Text.ToString()
Have you tried to run the stored procedure directly on the server with some dummy values? if so did it work sucessfully or did it throw an error?
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
The cmd.ExecuteNonQuery should return the number of rows affected by the command on UPDATE statement in this case if you are getting this message "Your info modification failed " means that for some reason the SP is not updating or is passing null values in the parameters could ya please share the content of the SP that way we can give a more precise solution.
|
|
|
|
|
when i connect Access database its connect properly but the data is saved on bin folder data base, and recored are not inserted n main database please help any one
my app.config file is
<configuration>
<configsections>
<connectionstrings>
<add name="EQMS.Properties.Settings.EQMSConnectionString" connectionstring="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\EQMS.mdb;Persist Security Info=True;Jet OLEDB:Database Password=ashaashwin"
="" providername="System.Data.OleDb">
<appsettings>
<add key="con" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=EQMS.mdb;Jet OLEDB:Database Password=ashaashwin;">
|
|
|
|
|
You might want to format your question a bit better - some of that got lost when displayed.
|
|
|
|
|
you need to explain the problem more clearly!
at a guess I would suggest that you look at the connection objects / strings that you are using to save the data at, I would use that as a starting point to solving this.
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
what type of error you getting.
|
|
|
|
|
I wanna to convert this string :
"
if("2" == "2" && "10" >="9")
return "true";
else
return "false";
if the above string contains any of these :
>= , <= , > , <
Replace the part of string ( "10" >= "9") TO :
( 10 >= 9)
So the final string would be :
if("2" == "2" && 10 >=9 )
return "true";
else
return "false";
How is it possible.
note : the string may contain lot of these math operation in it .
|
|
|
|
|
Create a token parser. So, start at the beginning of the string and work your way over it looking for the strings you're interested in. You could do this with a regex and some fancy parsing, but I would keep it simple if I were you. There are some shortcuts that you can take, such as only needing to store the right hand side of the token if the operators are the ones you are interested in. I would also look to split my string up into an array of smaller strings using the && and || operators. Then, I would perform my search on the smaller arrays.
|
|
|
|
|
If the text will contain proper spacing..You may use 'Contains ()'.
<pre>
string strMyText = "There is some text like 10 > 9";
if(strMyText .Contains("10 >9"))
{
strMyText = "I got it"
}</pre>
|
|
|
|