Grid view is not appearing when im running my website - c#

So i have a website hosted on blacknight.com. Im coding with cSharp and asp.net I have mySQL database stored within blacknight also.
However I want to add an admin section to my site where a user could log in and press a "load data" button and the registration table values from blacknight database would appear in a grid view.
However its just not working. Im wondering do I have to physically connect my gridview to my database? Because I have tried to connect to my hosted database with my gridview and it keeps saying it cant connect.
Below is the code behind my load data button. As as it stands when I upload my adminpages to blacknight and run, the gridview is not even appearing. This is my Fourth year project for college and I really need to get it working. Any help would be much appreciative.
protected void Button1_Click(object sender, EventArgs e)
{
string constring ="Server=xxxx; Database=xxxx; Uid=xxx; Pwd=xxx";
MySqlConnection conDb1317466_bk = new MySqlConnection(constring);
DataSet dbdataset = new DataSet();
//binding.DataSource = this.bindingSource.DataSource;
MySqlCommand cmdDb1317466_bk = new MySqlCommand("Select * from db1317466_bk.registration;", conDb1317466_bk);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDb1317466_bk;
DataTable dbdataset1 = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
//DataGridView1.DataSource = bSource;
DataGridView1.DataBind();
sda.Update(dbdataset);
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('Can Load data');</script>");
}
}
}

Try this one
GridView1.DataSource=dbdataset;
GridView1.DataBind();
Instead of using
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
DataGridView1.DataBind();
Also, I don't think that you should use:
sda.Update(dbdataset);

Try this
try
{
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmdDb1317466_bk;
DataTable dt= new DataTable();
da.Fill(dt);
DataGridView1.DataSource = dt;
DataGridView1.DataBind();
}

Related

GridView not appearing on webpage

I am building a web form. I have one search field and a search button. I am connecting to MS Access database to retrieve and display the result on the grid view. But my grid view is not appearing on the web page.
Can anyone please help me to find out where I am wrong?
Here is my aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Smita\\Desktop\\Project.accdb");
DataTable dt = new DataTable() ;
if (txtMerchant.Text.Length > 0)
{
con.Open();
OleDbDataAdapter DBAdapter = new OleDbDataAdapter();
DBAdapter.SelectCommand = new OleDbCommand("select * from Test where Merchant ID like '" + txtMerchant.Text + "%'", con);
DBAdapter.Fill(dt);
GridView1.DataSource = dt;
}
You have to call the DataBind, binding method first after you assign the data source.
Like that:
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.DataSource = dt; //Assigned a blank table.
"dt" Doesn't seem to point to anything.

Refreshing multiple datagridviews with single button click

Not sure if this question has been asked before, so here goes..
I have a front end app coded in C# windows forms. On a second form i have two datagridviews that gets populated from two different sql server pre-defined views
I need to refresh both datagrids at the same time with a single button click
My button click even looks like this..
private void RefreshBTN_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
How i understand this is, C# opens new connection, queries the DB and returns by filling datagridview1 with the required data. I would like the same click event to request data from another sql view and populate another datagridview at the same time. Visually both grids are aligned vertically on the same form, one on to of the other.
Many thanks in advance
Move the code for refreshing Grid1 into a separate function. Then copy paste and duplicate that function for Grid2. Change the SQL for Grid2 as well as the Grid2 name. Rename the copied function with 2. Then add a call to both functions so your button click will refresh both grids.
private void RefreshBTN_Click(object sender, EventArgs e)
{
//call both functions to refresh both on button click
RefreshGrid1();
RefreshGrid2();
}
private void RefreshGrid1()
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
//give this function a unique name to represent the second grid refresh
private void RefreshGrid2()
{
SqlConnection myConnection = new SqlConnection("removed for illustration only");
string query = "select * from daily_orders order by orderno DESC";
SqlCommand cmd = new SqlCommand(query, myConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
//rename this to your second grid name
dataGridView2.DataSource = dt;
}

Trouble With Displaying Data in DataGridView

I am trying to display my mysql database table on a DataGridView using c# . but nothings happening . please tell me where i've gone wrong in my code
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("Server = localhost; Database=project; user=root;password=''");
conn.Open();
string query = "SELECT * FROM billing";
MySqlDataAdapter bills = new MySqlDataAdapter(query, conn);
MySqlCommandBuilder cbuilder = new MySqlCommandBuilder(bills);
DataTable dtable = new DataTable();
bills.Fill(dtable);
//the DataGridView
DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dtable;
//set the DataGridView DataSource
dgView.DataSource = bSource;
bills.Update(dtable);
}

How to refresh DataGridView?

I am trying to force DataGrid to refresh its content on a form closing event. I have tried various methods, searched this forum for answers but still I am unable to find a solution.
This is how my DataGridView is being populated :
string strCon = ConfigurationManager.ConnectionStrings["ST"].ConnectionString.ToString();
string strSQL = "SQL QUERY";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = bindingSource1;
I have tried the Invalidate method, BindingSource.ResetBindings but still the GridView doesn't refresh. How do I force refresh it?
Data refresh
I think you are talking about data refresh. In this case you can just rebind your DatagridView to the source:
DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = aDataSource;
//Update Data in aDataSource
//...
dataGridView1 = null;
dataGridView1.DataSource = aDataSource;
In your code snapshot aDataSource should actually be bindingSource1. As #GIVE-ME-CHICKEN mentioned, in your case you should enclose the re-binding inside the proper callback to attach it to the right event:
private void Form1_FormClosing(object sender, FormClosingEventArgs e){ ... }
Data refresh
If you don't want to rebind your source you can adopt another solution: binding your dataGridView1 to a Collection Observable. In this case, it will be enough to implement the INotifyCollectionChanged interface to notify the change to its listeners (your DataGridView component)
Graphic refresh
If you are searching only for a graphic refresh, there is a specific method:
dataGridView1.Refresh();
Can you try and change this line:
bindingSource1.DataSource = table;
to
bindingSource1.DataSource = table.DefaultView;
Maybe doing it similar to this way helps?
public DataTable GetData(string SQLQuery)
{
string strCon = ConfigurationManager.ConnectionStrings["ST"].ConnectionString.ToString();
string strSQL = "SQL QUERY";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//Maybe only one of the clear code works.
dataGridView1.Rows.Clear();
dataGridView1.DataSource = null;
BindingSource bindingSource1 = new BindingSource();
bindingSource1.DataSource = GetData("SQL QUERY");
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.ReadOnly = true; //<-----------Try taking this off aswell
dataGridView1.DataSource = bindingSource1;
}

c# datagridview and mysql

I am doing some c# and mysql and I was successful at getting mysql data into a grid view for the first time! Now, my main question is, how do I manage the grid view style with this? For example, say I have already created columns and such, how do I put the mysql data into a specific column in the grid view?
Below is the code that is actually loading the data into the grid view.
try
{
conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
conn.Open();
// - DEBUG
// MessageBox.Show("Connection successful!");
MySqlDataAdapter MyDA = new MySqlDataAdapter();
MyDA.SelectCommand = new MySqlCommand("SELECT * FROM `swipes`", conn);
DataTable table = new DataTable();
MyDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
dataGridView1.DataSource = bSource;
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
MessageBox.Show(ex.Message);
Close();
}
Also, this creates columns based on the mysql data, how do I modify the width of these columns and such, or like stated above, use my own custom columns for my data? I've never done any mysql work in any UI, so I'm open to suggestions and tutorials as well. Thanks in advance!
If you really want to do this (as someone has already stated you should look at other options) you can create the columns in the designer and set the DataGridViewColumn.DataPropertyName on each column to the columns returned by the autogenerated dataset. Remember to turn of autogeneration of columns (AutoGenerateColumns) on the grid. This way you have full control of the column styles.
try this
string connection = "server=localhost;database=adil;user=root;password=";
MySqlConnection con = new MySqlConnection(connection);
con.Open();
MySqlCommand command = new MySqlCommand();
command.Connection = con;
MySqlDataAdapter MyDA = new MySqlDataAdapter();
string sqlSelectAll = "SELECT * from studentrec";
MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, con);
DataTable table = new DataTable();
MyDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
dataGridView1.DataSource = bSource;

Categories