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();
Related
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
I am using SQL to fill my datagridview. I am doing that this way:
string cn = ConfigurationManager.ConnectionStrings["Scratchpad"].ConnectionString;
SqlConnection myConnection = new SqlConnection(cn);
string sql = "some text here";
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, myConnection);
DataSet ds = new DataSet();
myConnection.Open();
dataadapter.Fill(ds, "Authors_table");
myConnection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Authors_table";
Now it deletes the old datagridview and paste the selection. But I want just to add the data to the column right of the existing data.
Thanks in advance
This requirement sounds like it could be met with a SQL Inner Join in the query you're using in your SqlDataAdapter. If you were able to join your two data sources together on matching keys, you could simply pull the data down once. But I'm going to assume there's some reason why that won't work for you.
You can in fact do what you want with a DataSet, but you'll need to take a few more steps:
string sql = "The first query"
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, myConnection);
DataSet ds = new DataSet();
myConnection.Open();
dataadapter.Fill(ds, "Authors_table");
myConnection.Close();
// I don't know what your primary key is, but you need to have one.
// I am assuming it's called "author_id".
DataColumn authorIdColumn = ds.Tables["Authors_table"].Columns["author_id"];
ds.Tables["Authors_table"].PrimaryKey = new[] { authorIdColumn };
// Get your second set of data
sql = "My second query, which also has the same primary key, but has more columns"
dataadapter = new SqlDataAdapter(sql, myConnection);
dataadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataSet ds2 = ds.Clone();
myConnection.Open();
dataadapter.Fill(ds2, "Authors_table");
myConnection.Close();
// Now we use the DataSet.Merge method, which is very powerful.
ds.Merge(ds2, false, MissingSchemaAction.AddWithKey);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Authors_table";
You may have to experiment with that a lot: this is just the outline. Without knowing your data and what you do up to this point, it's hard to know what settings or methods you might also need to call.
Try this. I ran multiple queries in one go
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT * FROM Customers; SELECT * FROM Orders", connection);
adapter.TableMappings.Add("Table", "Customer");
adapter.TableMappings.Add("Table1", "Order");
adapter.Fill(ds);
Sorry if this is a stupid question! I'm very new to coding and only have limited access to the internet whilst at work, I was just wondering if this is possible? and If so do you have a useful link for me to read over?
A quick overview of what I'm trying to do.
I have form1 and form2, form1 has a datagridview box that pulls data from the database, when I click on a cell it opens form2, which has a checklistbox which I want to now put code in so it pulls the data from a specific column and checks a box if the cell that was clicked applies to it.
Go easy on me if this question is stupid ^^, thanks in advance!
Some code I have produced so far! Which pulls the boxes from the database, but I also need to check the boxes too..
SqlConnection con = new SqlConnection("=");
con.Open();
string query = "select ID from dbo.report";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda;
DataSet ds = new DataSet();
sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
DataTable dt = ds.Tables[0];
foreach (DataRow datarow in dt.Rows)
{
checkedListBox1.Items.Add(datarow["ReportID"]);
Edited: put current coding in.
You can use below code to bind checkboxlist from database.
SqlConnection con = new SqlConnection("=");
con.Open();
string query = "select ID,NAME from dbo.report";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda;
DataSet ds = new DataSet();
sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
checkedListBox1.DataValueField = "ID"
checkedListBox1.DataTextField = "NAME"
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.DataBind();
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.