Can anyone please point out the cause of this error in the C# code of an ASP.Net application.
The build error is expecting a closing curly brace, although I have checked for all semicolons and brackets, but could not exactly pinpoint it. Any leads will be appreciated. Thanks!
Here is the code followed by the screen shot.
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Web;
using System.Data.SqlClient;
using System.Configuration;
namespace ANTrack
{
public class MyObjDataSource
{
public int iCount;
public string strName;
public void GetIncident(int IncidentID)
{
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
}
}
}
I think it has to do with the line:
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
You don't want to put a private in front of that, it's a variable in a method.
public void GetIncident(int IncidentID)
{
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
}
Remove private from variable declaration.
Corrected:
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Web;
using System.Data.SqlClient;
using System.Configuration;
namespace ANTrack
{
public class MyObjDataSource
{
public int iCount;
public string strName;
public void GetIncident(int IncidentID)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
}
}
}
you can not declare access modifiers like private in method, remove it
Related
Hello it is probably easy question for you, I'm a beginner and I'm making my own simple game and I want to use a Class:Gamer, which I want to initialize in MainWindow(Form1.cs) from a save file. From then, I want to use it on another Forms aswell, but somehow I can't make the instance go public.
Could you tell me what I'm doing wrong? Or is there another way how to solve this?
Thank you :)
Code on Form1:
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.Windows.Forms;
using System.Drawing.Text;
using System.IO;
namespace THE_GAME
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static Gamer Player;
private void MainWindow_Load(object sender, EventArgs e)
{
//load from savefile lvl;hp;money;gun;armor,name
string allData = File.ReadAllText("../../saveFile/save.txt");
string[] dataFromSave = new string[5];
dataFromSave = allData.Split(';');
Player = new Gamer(dataFromSave[0], dataFromSave[1], dataFromSave[2], dataFromSave[3], dataFromSave[4], dataFromSave[5]);
}
}
}
Code on secondForm2:
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.Windows.Forms;
using System.Drawing.Text;
namespace THE_GAME
{
public partial class Statistics : Form1
{
public Statistics()
{
InitializeComponent();
}
private void Statistics_Load(object sender, EventArgs e)
{
//labels stats
labelName.Text = Form1.Player.GetName();
labelHealth.Text = Form1.Player.GetHealth().ToString();
labelMoney.Text = Form1.Player.GetMoney().ToString();
}
private void buttonBack_Click(object sender, EventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
this.Close();
}
}
}
Thank you for your time.
To get at the Gamers Player object from a different Form just do
Form1.Player;
ie
var nam = Form1.Player.Name;
Form1.Player.Die();
etc
PS As I said in a comment - its extremely odd to dereive a form of yours from another one of your forms. Like this
public partial class Statistics : Form1
This question already has answers here:
Why does Property Set throw StackOverflow exception?
(3 answers)
Closed 2 years ago.
I'm using C# to develop a windows forms application and I required to store certain values (Ex: UserID and Role), in order to use them again in various forms throughout the application.
The User ID and the Role will be changed with each login.
So tried using static classes.
To test it out first, I did the following.
Created "Form1" with a textbox and a button.
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.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
common.text = textBox1.Text;
Form2 obj = new Form2();
obj.Show();
}
}
}
Then created "Form 2" with only a label.
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.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = common.text;
}
}
}
And to interconnect these two forms, created the following class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication3
{
public static class common
{
public static string text
{
get
{
return text;
}
set
{
text = value;
}
}
}
}
The purpose was to see if the label text on form2 would change when clicked on the button after entering text into the textbox in form1.
When running the code, the following error was thrown. Displays that this is thrown from the "set" method of the class.
An unhandled exception of type 'System.StackOverflowException' occurred in WindowsFormsApplication3.exe
If anyone could provide any clarity on this, it would be highly appreciated.
Thanks in advance.
Your set method is calling itself.
You need to add private string and change it and then return the changes via get.
Try this:
private static string _text;
public static string text
{
get
{
return _text;
}
set
{
_text = value;
}
}
I have two forms in a Windows Forms project: Form1 and aCurso.
I'm trying to send a List with objects of a class called curso (I mean: List<curso>) from Form1 to aCurso.
But Visual Studio show this:
Accessibility inconsistency: The type of parameter List<curso> is less accessible than the method aCurso.aCurso(List<curso>).
So, here is the code from Form1:
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.Windows.Forms;
namespace _18_05_18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<curso> cursos = new List<curso>();
private void btnAC_Click(object sender, EventArgs e)
{
Form f = new aCurso(cursos);
f.Show();
}
}
}
Here's code from aCurso:
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.Windows.Forms;
namespace _18_05_18
{
public partial class aCurso : Form
{
List<curso> cursos = new List<curso>();
public aCurso(List<curso> cursos)
{
InitializeComponent();
this.cursos = cursos;
}
}
}
Here's code from class curso:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18_05_18
{
class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}
}
You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.
All you have to do is make curso class public
public class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}
I am trying to make a program so if my Skype name/id is David it wont do anything but if its someone else Skype name/id it will change the RichMoodText I know how to change the RichMoodText but its just detecting if its a certain profile
Please Help
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.Windows.Forms;
using SKYPE4COMLib;
namespace Skype_Tools_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//RMT Means RichMoodText
string RMT = "Hi";
//CFN Means Creators FullName
string CFN = "David Fedrick";
Skype skype = new Skype();
skype.Attach();
//skype.CurrentUserProfile.RichMoodText = RMT;
if skype.CurrentUserProfile.FullName = CFN; <----
Cannot implicitly convert type 'string' to 'bool'
}
}
}
= is an assignment operator. You should use comparison operators in if statements, in this case ==. Also the statement should be in parenthesis, like
if (a == b)
{
\\ do your thing
}
i have problem in create class
i have 1 class called "lib" :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for lib
/// </summary>
public class lib
{
public static SqlConnection con;
public static void constr()
{
lib.con = new SqlConnection();
lib.con.ConnectionString = "Data Source=(local);Initial Catalog=FroumDB;Persist Security Info=True;User ID=sa;Password=123";
}
//insert
public static SqlCommand cmd;
public static void insert(string TableName ,string FieldNames, string values)
{
lib.constr();
lib.cmd = new SqlCommand("insert into " + TableName + " (" + FieldNames + ") values(" + values + ")", lib.con);
lib.cmd.CommandType = CommandType.Text;
lib.con.Open();
lib.cmd.ExecuteNonQuery();
lib.con.Close();
}
}
and in webform i used:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ForumPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string t = "TPost";
string f = "PostTitle,PostQuestion";
string v = "#pt,#pq";
lib.insert(t, f, v);
lib.cmd.Parameters.AddWithValue("#pt", this.tbxTitle.Text);
lib.cmd.Parameters.AddWithValue("#pq", this.tbxText.Text);
lblshow.Text = "submited !!!";
}
}
but when i play debug give me this error:
Must declare the scalar variable "#pt".
from this code "lib.cmd.ExecuteNonQuery()"
and this error in browser:
Must declare the scalar variable "#pt".
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Must declare
the scalar variable "#pt".
Source Error:
Line 27: Line 28: lib.con.Open(); Line 29:
lib.cmd.ExecuteNonQuery(); Line 30: lib.con.Close(); Line 31:
}
how can i fix this error ?
Try this:
var db = Database.open(/*your database*/);
string sqlcommand = "insert into TPost (PostTitle,PostQuestion,PostAnserw) values(#0,#1,#2)";
db.Execute(sqlcommand,param0,param1,param2);
There are also some security reasons to use #0,#1 like preventing sql injections.
i found answer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class ForumPost : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string t = "TPost";
string f = "PostTitle,PostQuestion";
string v = "#pt,#pq";
lib.insert(t, f, v);
lib.cmd.Parameters.AddWithValue("#pt", this.tbxTitle.Text);
lib.cmd.Parameters.AddWithValue("#pq", this.tbxText.Text);
lib.con.Open();
lib.cmd.ExecuteNonQuery();
lib.con.Close();
lblshow.Text = "submited !!!";
}
}