The ConnectionString property has not been initialized. Window Application - c#

I get the error.
The ConnectionString property has not been initialized.
My window application code is like below & the format is correct, but still i get error after i submit my data.
im using a LocalDataBase (My own local PC) for the table.
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.Configuration;
using System.Data.SqlClient;
namespace Tag_Number
{
public partial class Form1 : Form
{
string DBConn;
protected void Page_Load(object sender, EventArgs e)
{
DBConn = ConfigurationManager.ConnectionStrings[#"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString;
}
int InsertProduct()
{
using (SqlConnection myConnection = new SqlConnection(DBConn))
{
SqlCommand MyCommand = new SqlCommand("INSERT INTO NEW_SO_TAG_NUMBER (SOLine, SerialNbr, StatusCode, PackType, PalletID, PackingListNo) Values (#SOLine, #SerialNbr, #StatusCode, #PackType, #PalletID, #PackingListNo)", myConnection);
MyCommand.Parameters.AddWithValue("#SOLine", sOLineTextBox.Text);
MyCommand.Parameters.AddWithValue("#SerialNbr", serialNbrTextBox.Text);
MyCommand.Parameters.AddWithValue("#StatusCode", statusCodeComboBox.Text);
MyCommand.Parameters.AddWithValue("#PackType", packTypeComboBox.Text);
MyCommand.Parameters.AddWithValue("#PalletID", palletIDTextBox.Text);
MyCommand.Parameters.AddWithValue("#PackingListNo", palletIDTextBox.Text);
myConnection.Open();
return MyCommand.ExecuteNonQuery();
}
}
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Bla Bla Bla.",
"Info",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
}
private void nEW_SO_TAG_NUMBERBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.nEW_SO_TAG_NUMBERBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.tag_NumbersDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tag_NumbersDataSet.NEW_SO_TAG_NUMBER' table. You can move, or remove it, as needed.
this.nEW_SO_TAG_NUMBERTableAdapter.Fill(this.tag_NumbersDataSet.NEW_SO_TAG_NUMBER);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
serialNbrTextBox.ReadOnly = false;
MessageBox.Show("Remember to fill in your Bla Bla Bla.","Remind",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
}
else
{
serialNbrTextBox.ReadOnly = true;
}
}
private void packTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void statusCodeLabel_Click(object sender, EventArgs e)
{
}
private void statusCodeComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void packingListNoLabel_Click(object sender, EventArgs e)
{
}
private void packingListNoTextBox_TextChanged(object sender, EventArgs e)
{
}
private void palletIDLabel_Click(object sender, EventArgs e)
{
}
private void palletIDTextBox_TextChanged(object sender, EventArgs e)
{
}
private void serialNbrLabel_Click(object sender, EventArgs e)
{
}
private void serialNbrTextBox_TextChanged(object sender, EventArgs e)
{
}
private void sOLineLabel_Click(object sender, EventArgs e)
{
}
private void sOLineTextBox_TextChanged(object sender, EventArgs e)
{
}
private void packTypeLabel_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
InsertProduct();
}
}
}
after i insert the data i need to put into the table it keeps on pop-up this error.

Below line will not return the connection string
ConfigurationManager.ConnectionStrings[#"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString;
if you have connection string named as Target in your config file as below
<connectionStrings>
<add name="Target"
you can get the ConnectionString by the name
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Target"].ConnectionString))
{
}

Try the following things:
Try to give out your connection-string. the Name: <> is not correct.
Show us your Application-Config-File.
Are you sure that Page_Load is be called. (ASP.Net different from Windows-Forms)

Just shift your code to initialize the DBConn object from Page_Load function
protected void Page_Load(object sender, EventArgs e)
{
DBConn = ConfigurationManager.ConnectionStrings[#"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString;
}
To Form1_Load function,
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tag_NumbersDataSet.NEW_SO_TAG_NUMBER' table. You can move, or remove it, as needed.
DBConn = ConfigurationManager.ConnectionStrings[#"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString; //Do it here.....
this.nEW_SO_TAG_NUMBERTableAdapter.Fill(this.tag_NumbersDataSet.NEW_SO_TAG_NUMBER);
}
The Page_Load function as the name suggest is part of System.Web.UI but you are using System.Windows.Forms so the Page_Load function will not be invoked until you explicitly add event listener to FormLoad event. By default in System.Windows.Forms the function which is generated y clicking on load event is Form_Load but you can also change to Page_Load function.

Related

CS0501 error in Visual Studio 2017

So I have been coding C# for a few days, making a Roblox Exploit. I am making a drop down menu for my exploit, using timer ticks and it says "CS0501 C# must declare a body because it is not marked abstract, extern, or partial". I am a newbie at C# so I searched the problem up and I am not able to figure out why this problem is occurring. The code is down below. Please help, and if so, put down the code so I can just see what was my problem! Thanks!
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 WeAreDevs_API;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public void LuaTimer_Tick(EventArgs e, object sender);
private ExploitAPI wolfapi = new ExploitAPI();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
wolfapi.LaunchExploit();
}
private void buttonKill_Click(object sender, EventArgs e)
{
string target = usernameTarget.Text;
wolfapi.DoKill(target);
}
private void button1_Click_1(object sender, EventArgs e)
{
string target = usernameTarget.Text;
int speedValue = Int32.Parse(SpeedValue.Text);
wolfapi.SetWalkSpeed(target, speedValue);
}
private void button3_Click(object sender, EventArgs e)
{
string target = usernameTarget.Text;
wolfapi.TeleportMyCharacterTo(target);
}
private void speedValue_TextChanged(object sender, EventArgs e)
{
}
private void updateButton_Click(object sender, EventArgs e)
{
wolfapi.IsUpdated();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button1_Click_2(object sender, EventArgs e)
{
string script = luaExecutor.Text;
wolfapi.SendLimitedLuaScript(script);
}
private void luaC_Click(object sender, EventArgs e)
{
string script = luaExecutor.Text;
wolfapi.SendScript(script);
}
private void noDoors_Click(object sender, EventArgs e)
{
}
private void luaExecutor_TextChanged(object sender, EventArgs e)
{
}
private void luaPanel_Paint(object sender, PaintEventArgs e)
{
}
private void luaOpen_Click(object sender, EventArgs e)
{
}
private void LuaTimer_Tick_1(object sender, EventArgs e)
{
}
}
}
Well,
public void LuaTimer_Tick(EventArgs e, object sender);
is a method without body, so you should either implement it:
public void LuaTimer_Tick(EventArgs e, object sender) {
//TODO: put relevant code here
}
or, technically, you can mark it as abstract (do not forget to mark class with abstract as well: public abstract partial class Form1 : Form {...})
public abstract void LuaTimer_Tick(EventArgs e, object sender);

Bunifu cannot access non-static property Visible

I have been trying to make a menu which has 3 tabs. Login, News and website. I created user control and named it LoginTab.cs now I put the login panels in there LoginTab.cs, NewsTab. Now I am trying to make it visible and invisible depending on what tab people click. So if someone clicks news then the LoginTab goes away and shows the News tab. My problem is that I can't used LoginTab.Visible because its showing me "Cannot access non-static property 'Visible' in static context. How would I do this?
Main.cs
using System;
using System.Windows.Forms;
using UHWID;
namespace AnixLoader
{
public partial class Main : Form
{
bool username;
bool usergroup;
String SimpleUID = UHWIDEngine.SimpleUid;
String AdvancedUID = UHWIDEngine.AdvancedUid;
public Main()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Sdads_Click(object sender, EventArgs e)
{
}
private void PictureBox2_Click(object sender, EventArgs e)
{
}
private void CloseMenu_Click(object sender, EventArgs e)
{
Environment.Exit(0); // Closes Menu
}
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
LoginTab.Visible = true;
NewsTab.Visible = false;
}
private void News_Click(object sender, EventArgs e)
{
LoginTab.Visible = false;
NewsTab.Visible = true;
}
private void BtnMenu_Click(object sender, EventArgs e)
{
if (sidemenu.Width == 60)
{
//EXPAND
// 1. Expand the panel , w = 300
// 2. Show Logo
sidemenu.Visible = false;
sidemenu.Width = 300;
PanelAnimator.ShowSync(sidemenu);
LogoAnimator.ShowSync(Anix_Logo);
}
else
{
//MINIMIZE
//Using Bunifu Animator
// 1. Hide the logo
// 2. Slide the panel, w = 60
LogoAnimator.Hide(Anix_Logo);
sidemenu.Visible = false;
sidemenu.Width = 60;
PanelAnimator.ShowSync(sidemenu);
}
}
private void MainPanel_Paint(object sender, PaintEventArgs e)
{
}
}
}
LoginTab.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnixLoader
{
public partial class LoginTab : UserControl
{
public LoginTab()
{
InitializeComponent();
}
private void LoginTab_Load(object sender, EventArgs e)
{
}
private void LoginText_Click(object sender, EventArgs e)
{
}
private void bunifuImageButton1_Click(object sender, EventArgs e)
{
}
private void Login_Click(object sender, EventArgs e)
{
}
private void Password_Click(object sender, EventArgs e)
{
}
}
}
I have resolved the problem by changing from this
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{
LoginTab.Visible = true;
NewsTab.Visible = false;
}
private void News_Click(object sender, EventArgs e)
{
LoginTab.Visible = false;
NewsTab.Visible = true;
}
to this
private void BunifuFlatButton1_Click(object sender, EventArgs e)
{ // Log in button
this.MainPanel.Controls.Clear();
this.MainPanel.Controls.Add(new LoginTab());
}
private void News_Click(object sender, EventArgs e)
{ // News button
this.MainPanel.Controls.Clear();
this.MainPanel.Controls.Add(new NewsTab());
}

The process cannot access the file because it is being used by another process” Windows Application Development

I have seen similar questions however the answers didn't help me out. I am coding a basic windows application using c#. This is the 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.Windows.Forms;
namespace Practice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Main main = new Main();
main.Show();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
}
}
I don't know what I am doing wrong.
Main is a second window that is opened.
The other methods are there because I would have double clicked by mistake.
The following code solved my problem:
private void button2_Click(object sender, EventArgs e)
{
Main main = new Main();
this.Hide();
main.ShowDialog();
this.Close();
}
The problem as some of the people that tried to help was that due to the this.Hide() the program was still being executed even though I pressed the close (x) button.
But now with the above code the form is closed and no background processes are running after the app is closed.

How control opened tab in my web browser application?

Hi I'm new on C# and I made some simple Web Browser with tabs.
My problem is I don't know how to control selected tab. I mind if i click to back it go back on the selected tab.
Sorry for my bad English. This 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.Windows.Forms;
namespace HardRam
{
public partial class Form1 : Form
{
int i = 1;
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
}
private void button3_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/search?&q=" + textBox2.Text);
}
private void label2_Click(object sender, EventArgs e) { }
private void toolStripLabel1_Click(object sender, EventArgs e) { }
private void toolStripButton7_Click(object sender, EventArgs e)
{
TabPage newTp = new TabPage();
WebBrowser newWB = new WebBrowser();
newWB.Name = "Page" + tabControl1.TabPages.Count + 1;
newWB.Dock = DockStyle.Fill;
newWB.Url = new Uri(#"http://www.google.com");
newTp.Controls.Add(newWB);
tabControl1.TabPages.Add(newTp);
}
private void toolStripButton8_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/search?&q=" + textBox2.Text );
}
}
}
You make an instance of web-browser for each tab,so you have to get current tab and its web-browser control , so in.see my following example:
private void btnaddtab_Click(object sender, EventArgs e)
{
TabPage newTp = new TabPage();
WebBrowser newWB = new WebBrowser();
newWB.Name = "Page" + tabControl1.TabPages.Count + 1;
newWB.Dock = DockStyle.Fill;
newWB.Url = new Uri(#"http://www.bing.com");
newTp.Controls.Add(newWB);
tabControl1.TabPages.Add(newTp);
}
private void btnback_Click(object sender, EventArgs e)
{
(tabControl1.TabPages[tabControl1.SelectedIndex].Controls[0] as WebBrowser).GoBack();
}
private void btnforward_Click(object sender, EventArgs e)
{
(tabControl1.TabPages[tabControl1.SelectedIndex].Controls[0] as WebBrowser).GoForward();
}

errorProvider1 does not contain definition and no extension method

I just started learning C# and started with Windows Forms project.
When I try to validate text fields I get this error:
Error 1 'Downloader.SDownloader' does not contain a definition for 'errorProvider1' and no extension method 'errorProvider1' accepting a first argument of type 'Downloader.SDownloader' could be found (are you missing a using directive or an assembly reference?) C:\Users\Bad_Wolf\documents\visual studio 2010\Projects\Register\Register\Form1.cs 77 22 Register
There is my source code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Downloader {
public partial class SDownloader : Form {
public SDownloader() {
InitializeComponent();
}
private void fname_MouseClick(object sender, MouseEventArgs e) {
fname.Text = "";
}
private void fname_TextChanged(object sender, EventArgs e) {
}
private void lname_MouseClick(object sender, MouseEventArgs e) {
lname.Text = "";
}
private void lname_TextChanged(object sender, EventArgs e) {
}
private void username_MouseClick(object sender, MouseEventArgs e) {
username.Text = "";
}
private void email_MouseClick(object sender, MouseEventArgs e) {
email.Text = "";
}
private void password_MouseClick(object sender, MouseEventArgs e) {
password.Text = "";
}
private void password2_MouseClick(object sender, MouseEventArgs e) {
password2.Text = "";
}
private void password2_TextChanged(object sender, EventArgs e) {
}
private void register_Click(object sender, EventArgs e) {
}
private void fname_Validating(object sender, CancelEventArgs e) {
string errorMsg;
if (fname.Text.Trim().Length < 4) {
e.Cancel = true;
fname.Select(0, fname.Text.Length);
this.errorProvider1.SetError(fname, errorMsg);
}
}
private void fname_Validated(object sender, EventArgs e) {
}
}
}
The reason you are getting this error is because you haven't added the ErrorProvider to the form in the design view. Add the ErrorProvider and name it errorProvider1

Categories

Resources