I am trying to edit a cell in a RadGridView which has a datasource set from data from an SQL view.
What I was hoping to do was to populate the grid from the view, allow edits then manually update the associated tables when a user click an "Update" button.
As soon as the user enters data into a cell and leaves the cell is presents with an error "Specified method is not supported".
I assume it's trying to update the datasource with the new value so I am trying to work out how to tell it not to.
I am populating the table with:
using (SqlConnection con = new SqlConnection(mydatasource))
{
con.Open();
SqlCommand cmd = new SqlCommand("select SRID, Name, Result from EditBatchResultsView where SRID = " + drpSRID.Text, con);
SqlDataReader reader = cmd.ExecuteReader();
radGridView1.DataSource = reader;
}
According to RadGrid data binding documentation, the DataSource property accepts instances of the following types:
DataSet
DataTable
DataView
Array of DataRow
Any object collection that implements these interfaces:
IListSource
IList
IEnumerable
ICustomTypeDescriptor
Based from reference inspection, SqlDataReader doesn't supported because it doesn't implement those interfaces mentioned above, hence NotSupportedException has thrown when binding SqlDataReader contents into DataSource property directly.
As a workaround, you may create new DataTable instance and fill its contents from SqlDataReader like this:
var dt = new DataTable();
using (SqlConnection con = new SqlConnection(mydatasource))
{
con.Open();
SqlCommand cmd = new SqlCommand("select SRID, Name, Result from EditBatchResultsView where SRID = " + drpSRID.Text, con);
SqlDataReader reader = cmd.ExecuteReader();
// fill DataTable contents
dt.Load(reader);
// assign DataTable as data source instead
radGridView1.DataSource = dt;
}
// DataBind goes here
Note:
The string concatenation to build SQL query may prone to SQL injection. Using parameters when passing server control value to SQL query is more recommended way:
var dt = new DataTable();
using (SqlConnection con = new SqlConnection(mydatasource))
{
con.Open();
SqlCommand cmd = new SqlCommand("select SRID, Name, Result from EditBatchResultsView where SRID = #SRID", con);
cmd.Parameters.Add("#SRID", SqlDbType.VarChar).Value = drpSRID.Text;
SqlDataReader reader = cmd.ExecuteReader();
// fill DataTable contents
dt.Load(reader);
// assign DataTable as data source instead
radGridView1.DataSource = dt;
}
Related issue:
Populate data table from data reader
Related
I am new to coding and trying to learn but have come across an error which is very annoying, I am trying to populate a data grid with the value selected from a dropdownlist and I am not sure why it is throwing up this error.
The program takes and enters into the Database the information fine, but when I expanded on the program to add a Datagridview so the user could see the entered information for a specific driver selected from the combobox
using (SqlConnection sqlcon = new SqlConnection(connstring))
{
DataTable dt = new DataTable();
sqlcon.Open();
using (SqlCommand cmd = new SqlCommand("display", sqlcon))
{
cmd.Parameters.AddWithValue("#driver", driverSelect.SelectedValue);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
sqlcon.Close();
}
dataGridView1.DataSource = dt;
}
this is the error I get
"System.ArgumentException: 'No mapping exists from object type System.Data.DataRowView to a known managed provider native type.'" from this line. SqlDataReader reader = cmd.ExecuteReader();
Good day sir and ma'am. I'm planning to get all row header like "column_id and first_name, last_name and so on" and populate my listbox with row header from mysql database table. Please enlighten me how to do this. I am not sure what to use MySqlDataAdapter or MySqlDataReader. I'm beginner and willing to learn.
using (MySqlConnection conns2 = new MySqlConnection(connString))
{
conns2.Open();
MySqlCommand comm2 = new MySqlCommand("SELECT * FROM table1",
conns2);
MySqlDataAdapter add2 = new MySqlDataAdapter(comm2);
DataTable dt2 = new DataTable();
add2.Fill(dt2);
foreach(DataColumn column in dt2.Columns)
{
columnlistbox.Items.Add(dt2.Columns);
//it shows "collection" and exactly the number of row header
//but it doesn't show like "First name, client_id and last
//name"
}
conns2.Close();
}
for get columns names from a DataTable
If you need data from the db, and you load it into a DataTable (using DbDataAdapter or DbDataReader), you can know the column names from the DataTable.Columns collection property:
foreach (DataColumn col in dt2.Columns)
columnlistbox.Items.Add(col.ColumnName);
get only schema from MySql db (tables, columns ect)
but if you not need any data from the DB, but only its structure, you can use the DbDataReader. for load this data by the reader, a query must be "simulated" to the database:
using (MySqlConnection conns2 = new MySqlConnection(connString))
{
conns2.Open();
MySqlCommand comm2 = new MySqlCommand("SELECT * FROM table1", conns2);
using (var reader = MySqlCommand.ExecuteReader(CommandBehavior.SchemaOnly))
{
reader.Read();
var dtSchema = reader.GetSchemaTable();
foreach (DataRow row in tableSchema.Rows)
columnlistbox.Items.Add(row["ColumnName"]);
}
}
form https://stackoverflow.com/a/7159610/1271037:
Using the MySqlDataAdapter is the appropriate choice based on the sample code you provided because you are populating a DataTable and in ADO.NET DataAdapters are traditionally used to populate DataTables. A DataAdapter populates or "fills" your DataTable object.
You could also use a DataReader to populate your DataTable:
var dataReader = cmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(dataReader);
DataAdapters and DataReaders are a core design concept of ADO.NET, whether using Sql Server or MySQL database. A DataReader is a forward only, light and faster Database query helper. A DataAdapter is a bit heavier than a DataReader and also does more than a DataReader can do, it is possible that a DataAdapter uses a DataReader behind the scenes.
Here is how you would use the data from your MySQL database, you need to use a DataGrid, not a ListBox
using (MySqlConnection conn = new MySqlConnection(connString))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM table1",
conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
//display your dataset in a ASP.NET Web Forms DataGrid
dataGrid.DataSource = dt;
dataGrid.DataBind();
//conn.Close();// you do not need this, your using statements call Dispose and Dispose will release the connection back to the connection pool
}
You can get pointers from this code. In summary GetName is what you are looking for.
using (Common.DbCommand cmd = conn.CreateCommand()) {
cmd.CommandText = "SELECT * FROM table1";
dynamic dr = cmd.ExecuteReader();
int index = 0;
List<string> columns = new List<string>();
for (index = 0; index <= dr.FieldCount - 1; index++) {
columns.Add(dr.GetName(index));
}
}
I'm trying to take information from my SQL server and load it into a datagridview based on paramaters selected by the user. The example I post at the end of this question worked in an earlier function in the program, but now it isn't. This leads me to believe that the issue lies in the line that actually outputs the data to the DGV. Any thoughts on why it's not filling up the DGV? I've included two examples, neither of which is working. For some reason, they're just not inputting any information into the DGV, even though I know from debugging that they are indeed pulling the information from the server successfully.
SqlConnection DBConnection = new SqlConnection(ConnectionString);
//Opens the connection
DBConnection.Open();
//Creates a string to hold the query
string query = "SELECT * FROM PRD WHERE PRD_NUM LIKE '" +OutputBeforeIncrement + "%'";
//Creates an SQLCommand object to hold the data returned by the query
SqlCommand queryCommand = new SqlCommand(query, DBConnection);
//Uses the aforementioned SQLCommand to create an SQLDataReader object
SqlDataReader queryCommandReader = queryCommand.ExecuteReader();
//Creates a DataTable to hold the data
DataTable dataTable = new DataTable();
//This part actually loads the data from the query into the table
dataTable.Load(queryCommandReader);
dgvOutput.DataSource = dataTable;
The other example:
using (SqlDataAdapter newDA = new SqlDataAdapter(query, DBConnection))
{
DataTable Table = new DataTable();
newDA.Fill(Table);
dgvOutput.DataSource = Table;
}
You might try this or something similar:
SqlDataAdapter myDataAdapter;
SqlCommandBuilder myCommandBuilder;
SqlCommand mySqlCommand = new SqlCommand(myQuery, MySQLConnection);
//I think this is the default command type, and thus can be omitted
mySqlCommand.CommandType = CommandType.Text;
myDataAdapter = new SqlDataAdapter(mySqlCommand);
//Automates your insert/update/delete
myCommandBuilder = new SqlCommandBuilder(myDataAdapter);
myDataAdapter.Fill(myDataTable);
dgvOutput.DataSource = myDataTable.DefaultView;
I am pulling data from a sql server and putting it into a grid using c#. When the data displays on the grid, it is showing up as the guid rather than the actual name. How do I get the name to show and not the uniqe identifier. Any ideas? Thanks.
Here is some of the code:
public InventoryWindow()
{
InitializeComponent();
if (dgDataView != null)
{
SqlConnection con = new SqlConnection(connString);
SqlDataAdapter adpt = new SqlDataAdapter("select * from Item", con);
DataSet ds = new DataSet();
adpt.Fill(ds, "Item");
dgDataView.DataContext = ds;
//dgDataView.DataMember = "Item";
showdata();
}
}
private void showdata()
{
String connString = "server=server;database=database;user=user;password=password";
SqlConnection con = new SqlConnection(connString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from Item", con);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dgDataView.DataContext = dt;
con.Close();
}
You are using select * from Item and therefore returning all columns. You could just specify the columns you want in the Grid, in the order you want them. The grid by default has autocolumn generation on.
You can also specify the columns you want and what fields they map to using the columns DataMember values.
I figured this out, I just wrote my own query to display certain columns instead of automatically showing all of them.
Using VS2005 with C#
I want to fill the combobox by using the table value.
Code
OdbcConnection con = new OdbcConnection();
OdbcCommand cmd;
con.ConnectionString = "";
con.Open();
cmd = new OdbcCommand("Select no from table", con);
ada = new OdbcDataAdapter(cmd);
ds = new DataSet();
ada.Fill(ds);
combobox1.Items.Add(ds);
But no values was loading in the combobox, what wrong with my above mentioned code.
can any provide a solution for the probelm....
You do have something in your real connection string, right?
You're loading your data into a DataSet - that's a collection of tables and relations. How should the combobox know what table's data to display?? If the DataSet has multiple tables in it, you would have to additionally define which one of those to use. If the DataSet has only one table inside, then it's a waste of resources to use a DataSet in the first place.
If you only have a single set of data, use a DataTable instead:
con.Open();
cmd = new OdbcCommand("Select no from table", con);
ada = new OdbcDataAdapter(cmd);
DataTable data = new DataTable();
ada.Fill(data);
// define the column to be used to display text in the combobox
combobox1.DataTextField = "FirstName";
// define the column to be used as the value for the selection in the combobox
combobox1.DataValueField = "CustomerID";
combobox1.DataSource = data;
combobox1.DataBind();