I made this code with the help of tutorials now the problem is that I want Data Driven Auto Complete TextBox Using jQuery for ASP.NET Websites but when I run this code it works without any errors but it can not show the dropdown and can not show the data which is in OCRD table:
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.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Data.Sql;
using System.Web.Services;
namespace tutorialPoints
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List<string> GetCardNames(string empName)
{
List<string> Emp = new List<string>();
string query = string.Format("SELECT CardName FROM OCRD WHERE CardName LIKE '%{0}%'", CardName);
using (SqlConnection con = new SqlConnection("Data Source=1.2.3.4;Initial Catalog=TestData;Persist Security Info=True;User ID=abc;Password=abcd");
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Emp.Add(reader.GetString(0));
}
}
}
return Emp;
}
public static SqlConnection con { get; set; }
public static string CardName { get; set; }
}
}
I want output like this:
Related
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();
}
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();
}
}
}
}
I am trying to use .net connect to SQL EXPRESS database. I also get error Cannot open database "binarystream" requested by the login. The login failed.
but my dataconnect already show I connected to databaseenter
here is my C# 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;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection dee= new SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=binarystream;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand("INSERT INTO CUSTOMER VALUES(#USERNAME,#PASSWORD)",dee);
da.InsertCommand.Parameters.Add("#username", SqlDbType.VarChar).Value = textBox1;
da.InsertCommand.Parameters.Add("#password", SqlDbType.VarChar).Value = textBox2;
dee.Open();
da.InsertCommand.ExecuteNonQuery();
dee.Close();
}
}
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
This is my Sql functions class created in c#.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
namespace DatabaseTest
{
static class SQLFunctions
{
static private SqlConnection dbconnect = new SqlConnection(#"Data Source=sourcecode_guy-pc\sqlexpress;Initial Catalog=test;Integrated Security=True");
static public void Delete(object button) // This is the *Delete Function*
{
try
{
dbconnect.Open();
int IsDeleted;
SqlCommand DeleteAllQuery = new SqlCommand("DELETE FROM users", dbconnect);
DeleteAllQuery.ExecuteNonQuery();
IsDeleted = (int)DeleteAllQuery.ExecuteScalar();
if (IsDeleted == 0)
{
MessageBox.Show("Database has been truntcated.");
}
else if(IsDeleted == 1)
{
MessageBox.Show("An error has occured and your database table was not deleted.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
dbconnect.Close();
}
}
}
}
This is the delete button in my form
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 DatabaseTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDelete_Click(object sender, EventArgs e)
{
SQLFunctions.Delete(button: btnDelete);
}
}
}
When I run the program, it gives me a System.NullReferenceException: Object reference not set to an instance of an object.
at DatabaseTest.SQLFunctions.Delete(Object button) in
C:\my path :)
Your code Executes the Command twice.
DeleteAllQuery.ExecuteNonQuery();
IsDeleted = (int)DeleteAllQuery.ExecuteScalar();
Remove one or the other.
On form close I call update but I got error message: update requires InsertCommand whereas I inspired from this tut http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace datatablemsaccess
{
partial class Form1
{
OleDbDataAdapter dataAdapter;
private string getSQL() {
string sql = "SELECT * from tPerson";
return sql;
}
private string getConnectionString() {
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + #"App_Data\person.mdb";
return connectionString;
}
private DataTable getDataTable(string sql)
{
DataTable dataTable;
string connectionString = getConnectionString();
using (dataAdapter = new OleDbDataAdapter(sql, connectionString))
{
dataTable = new DataTable();
dataAdapter.Fill(dataTable);
}
return dataTable;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace datatablemsaccess
{
public partial class Form1 : Form
{
BindingSource bindingSource;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string sql = getSQL();
bindingSource = new BindingSource();
bindingSource.DataSource = getDataTable(sql);
dataGridView1.DataSource = bindingSource;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
dataAdapter.Update((DataTable)bindingSource.DataSource);
}
}
}
You need to define an InsertCommand.
See Update requires a valid InsertCommand when passed DataRow collection with new rows