Why my update with adapter doesn't work - c#

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

Related

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();
}

Cannot open database “binarystream” requested by the login. The login failed

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();
}
}
}

Form1 GridView is not accessible by Form 2 Button

I am trying to interact the DataGridView, located in Form1 (frPlanDeLucru), by a button in Form2. The reason for this is to create a separate search window. I keep getting Error CS0122, frPlanDeLucru.dataGridView1' is inaccessible due to its protection level.
Please help.
The Code in Form 1:
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.OleDb;
namespace Plan_de_lucru
{
[Serializable()]
public partial class frPlanDeLucru : Form
{
public frPlanDeLucru()
{
InitializeComponent();
}
public void TextBox1_TextChanged(object sender, EventArgs e)
{
}
public void ctrlLoad_Click(object sender, EventArgs e)
{
string constr = "Provider = MicroSoft.Jet.OLEDB.4.0; Data Source=" + TextBox1.Text + "; Extended Properties =\"Excel 8.0; HDR=Yes;\";";
OleDbConnection con = new OleDbConnection(constr);
OleDbDataAdapter sda = new OleDbDataAdapter("Select * From [" + textBox2.Text + "$]", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
new Form2().Show();
this.Show();
}
}
}
The code in Form2:
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 Plan_de_lucru
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void search_button_Click(object sender, EventArgs e)
{
String str = "select * from searchBox where ( Name like '%' + #search + '%')";
BindingSource bs = new BindingSource();
bs.DataSource = frPlanDeLucru.dataGridView1.DataSource; //<- Here is the problem, do not know the fix
}
}
}
In Form2:
public DataGridView Dgv { get; set; }
In Form1:
Form2 f = new Form2();
f.Dgv = dt; //Add this to the ctrlLoad_Click
In Form2 access its own Dgv propety.
You could add a reference to form a when creating form b and expose a public method / property in that "formA" that returns the datasource you wish. An example in which this is used:
public class FormA
{
public FormA()
{
}
public object GetDataSource()
{
return datagrid1.DataSource.ToObject();
}
}
public class B
{
private A _formA;
public B(A formA)
{
_formA = formA;
}
private void GetDataSourceFromA()
{
object dataSource = _formA.GetDataSource();
}
}
This might lead to another problem though; the second form being on a different thread. Calling objects on the first form from a different thread is not possible, more info here, though I think out of scope for your question.
Thank you for your help. I found that Form1 GridView was set to private... i entered in the code behind Form1 and modified it to private.
Cheers.

How to create Data Driven Auto Complete drop down TextBox?

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:

C# get set error

This Form 1
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.Diagnostics;
using System.Threading;
using Managed.Adb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
AndroidDebugBridge mADB;
String mAdbPath;
List<Device> devices = AdbHelper.Instance.GetDevices(AndroidDebugBridge.SocketAddress);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e )
{
//mAdbPath = Environment.GetEnvironmentVariable("PATH");
mAdbPath = "C:\\Users\\Nadun\\AppData\\Local\\Android\\android-sdk\\platform-tools";
mADB = AndroidDebugBridge.CreateBridge(mAdbPath + "\\adb.exe", true);
mADB.Start();
var list = mADB.Devices;
textBox1.Text = "" + list.Count;
foreach (Device item in list)
{
Console.WriteLine("");
listBox1.Items.Add("" + item.Properties["ro.build.product"].ToString() + "-" + item.SerialNumber.ToString() );
}
//Console.WriteLine("" + list.Count);
}
private void button2_Click(object sender, EventArgs e)
{
string text = listBox1.GetItemText(listBox1.SelectedItem);
Form2 f2 = new Form2(text);
// f2.Phone = "scs";
SetPhone sp = new SetPhone();
sp.PhoneModel = "Test";
this.Visible = false;
f2.ShowDialog();
}
}
}
This is Form 2
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
private string phone;
public string Phone
{
get { return this.phone; }
set { this.phone = value; }
}
public Form2(string a)
{
InitializeComponent();
textBox1.Text = a;
}
private void Form2_Load(object sender, EventArgs e)
{
//Form2 f2 = new Form2();
//f2.phone = "s";
//textBox1.Text = f2.Phone;
SetPhone sp = new SetPhone();
textBox1.Text = sp.PhoneModel;
Console.WriteLine("sefsef-"+sp.PhoneModel);
}
}
}
This is my Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
class SetPhone
{
private string phoneModel;
public string PhoneModel {
get { return this.phoneModel; }
set { this.phoneModel = value; }
}
}
}
Get always returning empty.i don't know why.
I am trying to set values from "form1".
i wrote class for that as well.but when i getting values from "form2" it returning empty.i don't know why
Your SetPhone class object which is calling the setter in the button2_click is a local variable, so when you try access the same in Form2_Load using another local variable, it is a completely new object and Get returns an empty string (default value). You should be able to share the SetPhone variable across forms, may be using constructor, then it will retain the values set using the setter

Categories

Resources