Error Message is not clear to me - c#

i Keep getting this error message and can't figure out why?
Compiler Error Message: CS1513: } expected
here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void gettickvalue(object sender, EventArgs e)
{
Random RandomNumber = new Random();
int n = RandomNumber.Next(1, 9);
imgBanner.ImageUrl = System.String.Concat("Timer/banner_", n.ToString(), ".jpg");
}

If what you posted is your whole class, you're missing a curly brace. Exactly as the error message says.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void gettickvalue(object sender, EventArgs e)
{
Random RandomNumber = new Random();
int n = RandomNumber.Next(1, 9);
imgBanner.ImageUrl = String.Concat("Timer/banner_", n.ToString(), ".jpg");
}
} //we added this curly brace
It's easier to see where it's missing if you format your code properly. Visual Studio can do this for you, look it up to see how in your version. I'm on Visual Studio 2010 Pro, so I go to Edit -> Advanced -> Format Document.
Notice also that I changed your concatenation function to just be String.Concat instead of System.String.Concat. No need to put the system reference in front of it because the namespace is already referenced by your first using statement.
I also suggest you rename your function. See General Naming Conventions on MSDN.

Related

Class in ASP.NET Partial Class in App_Code

I created a class for connection and using with register.aspx without any problem. When i try to move codes from register.aspx.cs to regpartial.cs then i get an conflict error: "connection conn = new connection();"
I would like to move codes register.aspx.cs to mypartial.cs. I think it will be better but i'm not sure how can i solve conflict problem.
Register.aspx.cs (final)
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using test.App_Code;
namespace test
{
public partial class register: System.Web.UI.Page
{
private void Page_Load(object sender, EventArgs e)
{
}
Connection.cs (final try)
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace test.App_Code
{
public class connection
{
public SqlConnection connect()
{
SqlConnection conn= new SqlConnection("Data Source=******;Initial Catalog=****;Integrated Security=False;User Id=****;Password=*****;MultipleActiveResultSets=True");
baglanti.Open();
return (conn);
}
}
regpartial.cs (Final try)
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace test.App_Code.partials
{
connection conn = new connection();
public partial class regpartial : register
{
}
}
In general you shouldn't have much code inside your aspx.cs files.
You want to separate the logic that is directly your view/presentation logic (stuff that makes the .aspx pages work) from your business logic (sequencing, transformation, validation), and finally you want to have your Data Access / Resource Access to be isolated.
I'd also recommend using Dapper.NET over raw ADO.NET/SqlConnection.
I'm going to string together a very basic example of how you can separate this. This is not guaranteed to compile c# code but it will very close pseudocode.
Inside registration.aspx.cs
private void btnRegister_Click(object sender, EventArgs e)
{
var email = txtEmail.Text;
var password = txtPassword.Text;
var registration = new Registration { Email = email, Password = password }
var bizService = new RegistrationService();
var response = bizService.Register(registration);
if(response.Success) Response.Redirect("~/registration/success");
ltlError.Text = response.FailureMessage;
}
RegistrationService.cs
public class RegistrationService {
public RegistrationResponse Register(Registration req)
{
var regDAL = new RegistrationAccess();
var isEmailDuplicated = regDal.DoesEmailExist(req.Email)
if(isEmailDuplicated)
return new RegistrationResponse {
Success = false,
FailureMessage = "Email exists, did you mean to login instead?
}
regDAL.InsertNewRegistration(req)
return new RegistrationResponse { Success = true };
}
}
Lastly you should have a RegistrationAccess.cs that contains only code for reading and writing to SQL Server / other database / file system.
Note how the aspx.cs file has no knowledge of RegistrationAccess. Your view should not be directly calling the database. The other thing to note is that RegistrationService has no knowledge of the view. It receives a Registration type, then executes business logic and calls out to the DAL. The DAL class will have zero knowledge of both the view and the RegistrationService, it will know only one thing, the database.
This is the basis of a very simple ASP.NET webforms solution. A better solution would use the MVP/MVVM patterns and the Dependency Inversion Principle but those aren't worth using if you don't understand basic separation of concerns yet.

c# error: input string was not in a correct format

I'm a beginner in c#, currently attempting a windows form project. I've designed a form titled drugform. I use the dataset method to connect to the database. Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;
using drugstoreform.BaseInfoTableAdapters;
namespace drugstoreform
{
public partial class DrugForm : Form
{
int Row = -1;
public DrugForm()
{
InitializeComponent();
}
private void Register_click(object sender, EventArgs e)
{
try
{
dbm_Medecine db = new dbm_Medecine();
db.Insert(Convert.ToInt32(DrugCode.Text.Trim()), DrugName.Text.Trim(), Convert.ToString(HowUse.Text.Trim()), Convert.ToDecimal(price.Text.Trim()));
}
catch(SqlException ex)
{
}
When I click on the register button, I get this error: input string was not in a correct format.
You get the error inConvert.ToInt32 and/or Convert.ToDecimal because the input was invalid. You can use int.TryParse and decimal.TryParse to validate it:
int drugCode;
decimal price;
if (int.TryParse(DrugCode.Text.Trim(), out drugCode) && decimal.TryParse(price.Text.Trim(), out price))
{
db.Insert(drugCode, DrugName.Text.Trim(), HowUse.Text.Trim(), price);
}
In the if drugCode and price are initialized with the correct value. Otherwise you should provide an error message that the user should provide correct input.
Possible reasons: perhaps the user enters 2.6 but the computer uses , as decimal separator. Or DrugCode.Text or price.Text are simply empty.

Why "threading" is unknown for visual studio?

I include using System.Windows; but threading still it doesn't exist in the namespace
?!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;
using System.Net;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows;
using System.Timers;
using Graduation_Project;
namespace GP
{
public partial class Register : System.Web.UI.Page
{
static int count = 0;
static int timer = 600;
protected void Page_Load(object sender, EventArgs e)
{
System.Windows.Threading.DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 600000); // 600000 Milliseconds
dt.Tick += new EventHandler(dt_Tick);
}
still have aproblem
System.Windows.Threading.DispatcherTimer is in WindowsBase.dll assembly. Make sure you have added reference to that assembly.
MSDN -
UPDATE -
DispatcherTimer is meant for WPF and Silverlight apps. It can't be used for asp.net project. Instead you should use Timer class meant for web projects.
DispatcherTimer class belongs on System.Windows.Threading namespace which it's belongs in WindowsBase.dll
Be sure you added the right assembly in your project. It is proably in C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 folder..

using Microsoft Translate ( GetTranslation Service )

I'm going to build website that uses the translation service from Microsoft. I need to get all the available translations for each word but the code I have provides only one translation while it is supposed to give all available translations. Here's the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using microsofttranslator;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.ServiceModel.Channels;
using System.ServiceModel;
using TranslatorService;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{string Appid="my Appid";
string t="any text";
microsofttranslator.TranslateOptions options = new microsofttranslator.TranslateOptions();
options.Category = "general";
options.ContentType = "text/plain";
options.User = "TestUserId";
options.Uri = "";
bool a=true;
SoapService s = new SoapService();
microsofttranslator.GetTranslationsResponse translations = s.GetTranslations(Appid, t, "ar", "en", 5,a, options);
Response.Write(string.Format("Available translations for source text '{0}' are", t));
foreach (microsofttranslator.TranslationMatch translationMatch in translations.Translations)
{
Response.Write(string.Format("Translated text: {0}" + Environment.NewLine + " Rating:{1}" + Environment.NewLine + "Count:{2}" + Environment.NewLine, translationMatch.TranslatedText, translationMatch.Rating.ToString(), translationMatch.Count.ToString()));
} }}
I added Microsft translate WSDL as a web reference http://api.microsofttranslator.com/v2/Soap.svc?wsdl and I added TranslatorService as a service reference http://api.microsofttranslator.com/V2/Soap.svc
This code works well but as I said it gives only one translation while it is supposed to give all the available translations of a word. I cannot figure out what I am doing wrong.
Maybe you should use the "translateArray" method.
http://msdn.microsoft.com/en-us/library/ff512438.aspx

C# and Microsoft Speech.Recognition and Speech.Synthesis

I'm new to C# and I'm new to Speech.Recognition.
I searched very long for tutorials but didn't find that much, I'm even not quiet sure whether I included everything correctly.
I downloaded:
SDK
Runtime
Languages
I'm programming local, I have Windows XP, .net framework 3.5.
Now I just want to get started with some simple lines of code, like to say "hello world" or say one or two words as input.
I tried following, and of course it doesn't work :>
error:
"The Typ- or Namespacename "SpeechSynthesizer" couldn't be found (Is a Using-Direktive or a Assemblyverweis missing?)"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using System.Speech.Synthesis;
namespace System.Speech.Recognition { }
namespace System.Speech.AudioFormat {}
namespace System.Speech.Recognition.SrgsGrammar{}
namespace System.Speech.Synthesis { }
namespace System.Speech.Synthesis.TtsEngine { }
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SpeechSynthesizer foo = new SpeechSynthesizer();
foo.Speak("Test");
}
}
}
edit:
hello,
i tried you code,but
using SpeechLib;
couldn't be found :>
well now i wrote:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.SpeechLib;
namespace System.SpeechLib { }
namespace System.Speech.Recognition { }
namespace System.Speech.AudioFormat {}
namespace System.Speech.Recognition.SrgsGrammar{}
namespace System.Speech.Synthesis { }
namespace System.Speech.Synthesis.TtsEngine { }
but I get an error with:
numericUpDown1,SpVoice,SpeechVoiceSpeakFlags,textBox1 and Timeout
Project + Add Reference, .NET tab, select "System.Speech".
A project template pre-selects several .NET assemblies. But only common ones, like System.dll, System.Core.dll, etcetera. You have to add the 'unusual' ones yourself.
you can try this:
get Interop.SpeechLib.dll
using SpeechLib;
private void ReadText(string readText)
{
int iCounter = 0;
while (Convert.ToInt32(numericUpDown1.Value) > iCounter)
{
SpVoice spVoice = new SpVoice();
spVoice.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
spVoice.WaitUntilDone(Timeout.Infinite);
iCounter = iCounter + 1;
}
}

Categories