C# MySql DataGrid Updater - c#

This is my Code to View Data from DataBase into Grid View. Can Anyone help me how to update data into database through updating data in Same GridView
Heres My Code
string myConnection = "datasource=localhost;port=3306;username=root;password=2905";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand cmdDataBase = new MySqlCommand("select * from mydb.stud_info where stud_id = '" + this.studentid_txt.Text + "'; ", myConn);
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
grid1.DataSource = bSource;
sda.Update(dbdataset);

this is a faster method just one line and it will update everything in your datagridview
this.vesselTableTableAdapter.Update(this.dockMasterDbDataSet.VesselTable);

Related

Search Box not working

When I type in my search box my Data grid view doesn't show the data that i typed in even though its in the database. This is my codes for the search box
MySqlConnection con = new MySqlConnection(MyConnectionString);
con.Open();
MySqlDataAdapter adapt = new MySqlDataAdapter();
con = new MySqlConnection(MyConnectionString);
adapt = new MySqlDataAdapter("SELECT * from tblregister where FirstName like '" + textBox1.Text + "%'", con);
DataTable ds = new DataTable();
DataSet dt = new DataSet();
adapt.Fill(dt);
dataGridView1.DataSource =ds;

C# NO Mapping exists from object type systems.windows.forms.textbox

I know its a in here somewhere but I cannot find the error:
txtDealID.Text = Properties.Settings.Default.CurDealNo.ToString();
string server = Properties.Settings.Default.SQLServer;
string constring = "Data Source=" + server + ";Initial Catalog=Propsys;Persist Security Info=True;User ID=sa;Password=0925greg";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand("SELECT * FROM Agents WHERE DealID=#DealID", conDataBase);
cmdDataBase.Parameters.AddWithValue("#DealID", txtDealID);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);
Your problem is here:
cmdDataBase.Parameters.AddWithValue("#DealID", txtDealID)
In fact, here you're passing to SqlCommand not the text entered into your textbox, but textbox itself.
It should be
cmdDataBase.Parameters.AddWithValue("#DealID", txtDealID.Text)
Also, avoid using AddWithValue and use Add method override with explicit type of parameter.
See Can we stop using AddWithValue already article describing possible problems can occur while using AddWithValue.

Error displaying data from DB to predefined columns of datagrid View on button click event

I am using the following code:
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);
BindingSource bindsrc = new BindingSource();
DataGridView datagridv = new DataGridView();
bindsrc.DataSource = dt;
datagridv.DataSource = bindsrc;
connection.Close();
I have three predefined columns in datagridview1 say 'PLU_Code', 'Barcode' and 'Product_Name'.
I want to display the result (more than one row) of the select query in the datagridview. But I am not getting any.
How do I achieve this?
Try this code:
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
datagridview1.DataSource = table;
connection.Close();
I got the result. I achieved two empty rows on using #user4340666 code. when i scrolled the grid i found the set of data's been added as new columns leaving predefined column cells empty. so i just cleared the columns.
dataGridView1.Columns.Clear();
string getinputs = "SELECT ir.plu_code PLU_Code,ir.barcode Barcode,ir.product_name Product_Name FROM inventory_register ir,inventory_value iv WHERE ir.dept_id='" + textBox1.Text + "' AND ir.barcode = iv.barcode";
connection.Open();
MySqlDataAdapter dataAdapter = new MySqlDataAdapter(getinputs, connection);
MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
dataAdapter.Fill(table);
datagridview1.DataSource = table;
connection.Close();
Try this code:
string getinputs = "Select Query";
connection.Open();
MySqlCommandBuilder cmdbuilder = new MySqlCommandBuilder(getinputs,connection);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmdbuilder);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataGridView datagridv = new DataGridView();
datagridv.DataSource = dt;
datagridv.DataBind();
connection.Close();

Using Select query to display data in a data grid view

I am trying to display a table data of my db in a DataGridView.here is my code
private void button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Data Source=SHIRWANIPC;" +
"Initial Catalog=TEST DATABASE;" + "Integrated Security=True");
myConnection.Open();
SqlCommand objcmd = new SqlCommand("SELECT * FROM Customer", myConnection);
SqlDataAdapter adp = new SqlDataAdapter(objcmd);
DataTable dt = new DataTable();
//adp.Fill(dt);
DataGridView.DataSource = adp.Fill(dt);
}
The code is not giving any error now but but it dosent display the data from my table in my grid ?
You are using class name instead of object (instance) name of DataGridView, check in the html what is ID/name of DataGridView, It is probably Win Forms and you do not have DataBind method there. DataBind method is defined for GridView for ASP.net. Find more about DataGridView here.
DataGridViewObject.DataSource = adp.Fill(dt);
this is the answer to my quetion , hope it helps others who are new to this
SqlConnection myConnection = new SqlConnection("Data Source=SHIRWANIPC;" + "Initial Catalog=TEST DATABASE;" + "Integrated Security=True");
myConnection.Open();
SqlCommand objcmd = new SqlCommand("SELECT * FROM Customer", myConnection);
//objcmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(objcmd);
DataTable dt = new DataTable();
adp.Fill(dt);
//MessageBox.Show(dt.ToString());
dataGridView1.DataSource = dt;
Create an instance of DatagridView:
DataGridView dview = new DataGridView();
....
dview.DataSource = adp.Fill(dt);
dview.DataBind();
You are directly accessing DataGridView properties and tryingto add data to the class. You need to create and an instance of a DataGridView first. Then assign the datable to the object.
Than bind the data to the DataGridView, otherwise the data is not bound to the control and will not be displayed.
DataGridView view = new DataGridView();
dview.DataSource = adp.Fill(dt);
dview.DataBind();
You are not exectuting the query
myConnection.Open();
SqlCommand objcmd = new SqlCommand("SELECT * FROM Customer", myConnection);
DataTable dt = new DataTable(); using
(SqlDataReader sqlDataReader = objcmd.ExecuteReader())
{
dt.Load(sqlDataReader);
sqlDataReader.Close();
}
DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = dt;

How to solve a datasource error in Microsoft report viewer?

i am having Report page in my windows project .In that i included microsoftReportViewer . In that page two combobox is there .items in 1st combobox are:
FirmDetails
ChittyHolding
LoanDetails.
Corresponding to selection this items in combobox1, items in combobox will change .I need report According to this values in 2 combbox.
My code is like
if (cbReprt.Text == "FirmDetails")
{
if (cbGeneral.Text == "AllFirmDetails")
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
allfirmdetails ds = new allfirmdetails();
string str = "Select * from View_2";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
DataTable dt = new DataTable();
da.Fill(dt);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\Report7.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("allfirmdetails_View_2", dt));
reportViewer1.RefreshReport();
}
else
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
allfirmdetails ds1 = new allfirmdetails();
string str1 = "Select * from View_2 where FirmName='" + cbGeneral.Text + "'";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(ds1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\firmwise.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("allfirmdetails_View_2", dt1));
reportViewer1.RefreshReport();
}
}
else if (cbReprt.Text == "ChittyDetails")
{
if (cbGeneral.Text == "AllChittyDetails")
{
reportViewer1.RefreshReport();
SqlConnection con = new SqlConnection("Data Source=202.88.231.102;Initial Catalog=dbs_Merchant;Persist Security Info=True;User ID=sa;Password=abc123*");
con.Open();
ChittyDetails ds2 = new ChittyDetails();
string str2 = "Select * from View_1";
SqlCommand cmd2 = new SqlCommand(str2, con);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
da2.Fill(ds2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = "F:\\MerchantAssociation\\MerchantAssociation\\allchitty.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("ChittyDetails_View_1", dt2));
reportViewer1.RefreshReport();
}
First I select chittyholding details. Then I got the report. Then I select chitty details report, I got the error like
**An error occured during local report processing .A data sourcr instance has not been supplied for the data source "ChittyHolding_View_7"**. If I close and run the project again ,then that firmdetails selection will work.But another will not work.It means Only for one selection I am getting the report. Why? Please solve this error
Try to call reportViewer1.Reset(); instead of reportViewer1.Refresh(); at the beginning of every condition

Categories