I am trying to populate some text boxes on a form with data pulled from MySQL into a data set.
I cant seem to get the code right and could use some help
string ConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
MySqlConnection connection;
MySqlDataAdapter adapter;
DataTable DTItems;
connection = new MySqlConnection(ConnectionString);
try
{
//prepare query to get all records from items table
string query = "select * from spt_proposal where fr_Numer = "+ a+"";
//prepare adapter to run query
adapter = new MySqlDataAdapter(query, connection);
DataSet DS = new DataSet();
//get query results in dataset
adapter.Fill(DS);
textBox5.Text = DS.Tables[0].Rows[0].ToString();
This does not hit the text box at all.
a in the query is a variable that is pulled from a different form
and the query should pull 32 different things from the database in a row
There are about 9 text boxes on the form that i will have to fill with different data from this row.
Anyone have a better way to do this?
Brent
Try:
textBox5.Text = DS.Tables[0].Rows[0][0].ToString();
Remember, that a Table is pretty much a two dimensional array. Your code literally gets to the 1st row in the DataSet, but doesn't take into account the Column.
Related
I am trying to populate a datagrid table and add an additional combobox in a Winforms application to allow the user to select from an exception list.
The datagrid is populated using a Stored Procedure on SQL server.
(NOTE: due to my ITs security I have to go through a single server using linked servers to where the data is to query the data so the stored procedure uses dynamic SQL)
The data pulls properly and the additional combobox appears however once I select an exception on 1 row and try to go to the next row, the first combobox's selection disappears.
An additional question, given the nature or how I query the data, how would I update the data on the original sql tables from datagrid? Another stored procedure?
FYI, I'm a SQL developer but I'm fairly new to C#
My code is below for the datagridview method. Thanks in advance.
public void displayDataGridView(string param)
{
SqlConnection con = new SqlConnection("Data Source = SQLServer1; initial catalog=GlbDBNames; integrated security=true ");
{
SqlCommand cmd;
cmd = new SqlCommand(param, con);
cmd.CommandTimeout = 60 * 20;
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dg_batchsummary.DataSource = ds.Tables[0];
dg_batchsummary.Columns[0].Width = 200;
dg_batchsummary.AutoGenerateColumns = false;
dg_batchsummary.AllowUserToAddRows = false;
DataGridViewComboBoxColumn ExceptionList = new DataGridViewComboBoxColumn();
ExcptionList.HeaderText = "Exception List";
ExceptionList.Name = "Newexcept";
ExceptionList.Items.AddRange("Item1","Item2","Item3");
dg_batchsummary.Columns.Add(ExceptionList);
}
I was able to connect to the powerpivot datamodel with amo:
string ConnString =
"Provider=MSOLAP;Data Source=$Embedded$;
Locale Identifier=1033;Location=" + ThisAddIn.Current.Application.
ActiveWorkbook.FullName + ";SQLQueryMode=DataKeys";
Microsoft.AnalysisServices.Server OlapServer =
new Microsoft.AnalysisServices.Server();
and I am able to retrieve the column names but nothing else. How can I fill a dataset from the model or even how can I view the cell values within this datamodel? I have attempted this
AdomdConnection Conn = new AdomdConnection(ConnString);//using the above string
Conn.Open();
command = new AdomdCommand("SELECT [Table1].[x] ON COLUMNS FROM [Model]",Conn);
adapter = new AdomdDataAdapter(command);
adapter.Fill(dataset);
but all that shows up is the number 1 as the contents no matter what is in the datamodel.
Using a DAX query instead of a MDX query solved the problem.
I have this code where I put data to the DataTable from where I show everything on DataGridView.
But when I look it contains information which supposed to be in file but its repeated twice.
Code to retrieve data from mysql database:
MySqlDataAdapter mySqlDataAdapter;
DataSet DS0 = new DataSet();
DataTable DT0;
string gender;
private void Filter()
{
ViewG.DataSource = null;
ViewG.Rows.Clear();
command.CommandText = "SELECT * FROM `table2` WHERE s1q2 = #gender";
command.Parameters.Add("#gender", MySqlDbType.VarChar);
command.Parameters["#gender"].Value = gender;
DT0 = DS0.Tables.Add("1Filter");
mySqlDataAdapter = new MySqlDataAdapter(command.CommandText, connection);
connection.Open();
mySqlDataAdapter.SelectCommand = command;
mySqlDataAdapter.Fill(DS0.Tables["1Filter"]);
ViewG.DataSource = DS0.Tables["1Filter"];
connection.Close();
}
Initially, on the start it retrieves all information from the database code (SELECT * FROM table) and displays on the DataGridView. And it works fine, but when I try to use filters to retrieve only for example "Females" problem occurs.
For full data I use:
mySqlDataAdapter.Fill(DS0.Tables["Full"]);
ViewG.DataSource = DS0.Tables["Full"];
For Filtered data:
mySqlDataAdapter.Fill(DS0.Tables["1Filter"]);
ViewG.DataSource = DS0.Tables["1Filter"];
If I run query used for filter on the application startup it does not duplicate and show correctly.
EDIT: SOLVED
From the code posted here, gender string is not assigned any value. So, your query be applying any filter that you want.
Thanks for you effort I found where the problem was. I used temp table on MySql and for some reason server did not dropped this table after connection is closed. So on the new query it added same items on the same table....
I have a stored procedure called "WEB_SEL_SECURITY_QUESTIONS" in my sql database which returns a table as follows.
SEQURITY_QUESTIONS_KEY, KEYCODE, QUESTION
I need to populate a html selectbox with the list of questions I get from executing the above procedure.
This is what I have tried
String s = ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
SqlConnection con = new SqlConnection(s);
con.Open();
SqlDataAdapter myAdepter = new SqlDataAdapter("WEB_SEL_SECURITY_QUESTIONS", con);
myAdepter.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
myAdepter.Fill(ds, "WEB_SEL_SECURITY_QUESTIONS");
String questions = ds.Tables["WEB_SEL_SECURITY_QUESTIONS"].Rows[]["QUESTION"].ToString();
securityQuestion1.DataSource = questions;
securityQuestion1.DataBind();
But this populates the select box with only one record and it generates the list items in such a way that, only one character per list item and only the first record.
You could try:
securityQuestion1.DataSource = ds.Tables["WEB_SEL_SECURITY_QUESTIONS"];
securityQuestion1.DataTextField = "QUESTION";
securityQuestion1.DataValueField = "KEYCODE"; // Or whatever the value field needs to be.
securityQuestion1.DataBind();
What your first attempt was doing is just taking that single value from one question and using that as the entire data source. So what you need to aim for is make the whole security questions table the data source, and then give it hints as to what columns to use for value and display (DataTextField, DataValueField).
Below is my Datagridview code to get the data from employee table.
the problem am facing is ,my employee table have 10 columns (ID,emplNo,Dob,JoingData...etc)
i just want to fill my grid with only ID,EmplyNo and DOB.
but the below code get everything,please advise me what i suppose to do to get only particular column
string sql = "select * from Employee";
SqlConnection connection = new SqlConnection(CONNECTION_STRING);
//SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
dataadapter = new SqlDataAdapter(sql, connection);
// DataSet ds = new DataSet();
ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, scrollVal, 5, "Employee");
connection.Close();
dgMessages.DataSource = ds;
dgMessages.DataMember = "tEmployee";
instead of
string sql = "select * from Employee";
do
string sql = "select ID, EmplyNo, DOB from Employee";
either change your select to only get the supset of what you want or use the designer:
it's a bit of a pain because you first have to add an unbound column and then edit the just added column to setthe DataPropertyName to a column-name in your resulting table - but it works.
You find this dialogs by clicking the "..." in the "columns" property of the PropertyEditor for the DataGridView or by clicking the little "Play"-Button on the top-right of the Grid in the designer (when it is selected)
Almost forgot: IMPORTANT: you need to add EVERY column and set Visible=False on those you don't want to see - I think this is different in WebForms where there is something like AutogenerateColumns.