Error while connecting to SQL Server from an UWP application - c#

I'm getting an error when trying to connect to SQL Server from the UWP application, I already have activated the capabilities:
Internet (Client & Server)
Internet (Client)
Private Networks (Client & Server)
The code is the following:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Data.SqlClient;
namespace App1544
{
/// </summary>
public sealed partial class MainPage : Page
{
const string ConnectionString = "SERVER = DESKTOP-DI0OR53; database= DBNAVEGANTE; user id= root; password = a";
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
using(SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
label1.Text = con.State.ToString();
}
}
}
}

Related

Getting error "The Type or Namespace name 'DBAccess;" cannot be found

For my class "WindowsFormsApp1", I'm running into a problem which occurs when trying to access my "DBAccess" class.
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 WindowsFormsApp1
{
public partial class Signin : Form
{
DBAccess objDbAccess = new DBAccess(); //where error occurs
DataTable dtUsers = new DataTable();
even when i try to add "using DBAccess;" I run into an error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace WindowsFormsApp1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseProject
{
class DBAccess
{
private static SqlConnection connection = new SqlConnection();
private static SqlCommand command = new SqlCommand();
private static SqlDataReader DbReader;
private static SqlDataAdapter adapter = new SqlDataAdapter();
public SqlTransaction DbTran;
private static string strConnString = "Data Source=(local);Initial Catalog=SocialNetwork;Integrated Security=True";
Could someone help me understand what i'm doing wrong here? I've been fiddling with this for a while trying to find out and cant seem to get it.
Add using WindowsFormsApp1.DatabaseProject; to SignIn class
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.DatabaseProject;
namespace WindowsFormsApp1
{
public partial class Signin : Form
{
// ......
}
}
And replace
namespace WindowsFormsApp1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace DatabaseProject
{
class DBAccess
// .......
}
with
namespace WindowsFormsApp1.DatabaseProject
{
public class DBAccess
{
// ....
}
}

Connect C# to mySQL database using windows form

im new to C# and trying to connect a windows form to a mySQL database.
The problem is i get the error below and can't find a way to fix this.
Error CS0120 An object reference is required for the non-static field, method, or property 'Login.con()
This is the Form.cs
using MySql.Data.MySqlClient;
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 Log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void BtnConnect_Click(object sender, EventArgs e)
{
Login.con();
}
}
}
This is the Login.cs file where the connections should be made.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using MySql.Data;
using System.Data;
namespace Log
{
public class Login
{
public MySqlConnection connection;
private string server;
private string database;
private string user;
private string password;
private string port;
private string connectionString;
private string sslM;
public Login()
{
server = "localhost";
database = "test";
user = "root";
password = "root";
port = "3306";
sslM = "none";
connectionString = String.Format("server={0};port={1};user id={2}; password={3}; database={4}; SslMode={5}", server, port, user, password, database, sslM);
connection = new MySqlConnection(connectionString);
}
public void con()
{
try
{
connection.Open();
MessageBox.Show("successful connection");
connection.Close();
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message + connectionString);
}
}
}
}
you need to instance Login class...
Login login = new Login();
login.con()
You have to create a new instance of class Login. Try like:
public void BtnConnect_Click(object sender, EventArgs e)
{
Login login = new Login();
login.con();
}

How to display Link Elements from an rss.xml in my C#

How to display the Hyperlink, Thumbnail, and Publish Date from the following RSS.XML Feed: https://www.economist.com/europe/rss.xml
I am trying to have the Hyperlink, Thumbnail and Publish Date from the feed above. How to go about this?
Below is my code that is displaying only the Title and Description:
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.Xml;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.ServiceModel.Syndication;
namespace RSS_Demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
XmlReader FD_readxml = XmlReader.Create(textBox1.Text);
SyndicationFeed FD_feed = SyndicationFeed.Load(FD_readxml);
TabPage FD_tab = new TabPage(FD_feed.Title.Text);
tabControl1.TabPages.Add(FD_tab);
ListBox FD_list = new ListBox();
FD_tab.Controls.Add(FD_list);
FD_list.Dock = DockStyle.Fill;
FD_list.HorizontalScrollbar = true;
foreach(SyndicationItem FD_item in FD_feed.Items)
{
FD_list.Items.Add(FD_item.Title.Text);
FD_list.Items.Add(FD_item.Summary.Text);
FD_list.Items.Add("---------");
}
}
catch { }
}
}
}

csharpcodeprovider: Can't use Process.Start()

So I'm making a WPF application where I'm using CSharpCodeProvider to compile code stored in string. Everything works great except for one part. When I try to use Process.Start(), it gives me "Metadata file 'System.Diagnostics.dll' could not be found" error. I don't know what that means.
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Dynamically_compile_codes
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", txtFrameWork.Text } });
Button button = (Button)sender;
CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll","System.Core.dll","System.Diagnostics.dll"}, txtOutput.Text,true);
//generate exe, not dll
parameters.GenerateExecutable = true;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox.Text);
if (results.Errors.HasErrors)
{
results.Errors.Cast<CompilerError>().ToList().ForEach(error=> txtStatus.Text+=error.ErrorText+"/r/n");
}
else
{
//If we clicked run then launch our EXE
Process.Start(txtOutput.Text);
}
}
}
}
And here is the code that is stored in the string:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Hidden_Console_Application
{
class Program
{
static void Main(string[] args)
{
Process.Start("explorer.exe");
}
}
}
System.Diagnostics is a namespace that resides in the System.dll assembly. The CompilerParameters constructor expects a string collection of assembly names and is therefore looking for a loaded assembly named System.Diagnostics which does not exist.
Should be:
CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll","System.Core.dll","System.dll"}, txtOutput.Text,true);

Windows phone back button closes app instead of going back a page

Working on a basic Windows Universal app and currently have the issue where upon pressing back on the details page of my lists it doesn't take me back to the main page but instead closes the app entirely.
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.Phone.UI;
using Windows.UI.Xaml.Navigation;
namespace MoreUniversalAppWork
{
public sealed partial class MainPage : Page
{
public QuoteReader qr = new QuoteReader();
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
qr.DownloadQuotes();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private void Hardware_Back(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
if (Frame.Content is ViewQuote)
{
Frame.Navigate(typeof(MainPage));
e.Handled = true;
}
}
private void LoadQuotes(object sender, RoutedEventArgs e)
{
FillList();
}
private void FillList()
{
// Downloads the JSON file with the quotes and filles List //
qr.DownloadQuotes();
var quotes = qr.quotes_dictionary();
list_quotes.Items.Clear();
foreach (var q in quotes)
{
list_quotes.Items.Add(q.Value);
}
if (list_quotes.Items.Count > 0)
{
list_quotes.ScrollIntoView(list_quotes.Items[0]);
}
}
private void ViewQuote(object sender, SelectionChangedEventArgs e)
{
// Upon clicking quote in list, details page is opened //
Frame.Navigate(typeof(ViewQuote), list_quotes.SelectedValue);
}
}
}
ViewQuote.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ComponentModel;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Phone.UI.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556
namespace MoreUniversalAppWork
{
public sealed partial class ViewQuote : Page
{
string quote;
public ViewQuote()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
quote = e.Parameter as string;
quote_display.Text = quote;
}
}
}
This is normal in Windows Phone 8.1. You may want to have a workaround in App.xaml.cs file.
public App()
{
this.InitializeComponent();
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame != null && rootFrame.CanGoBack)
{
e.Handled = true;
rootFrame.GoBack();
}
}
WP8.1 back button quits the app

Categories

Resources