Copying a MySQL search query into a Data table - visual-c++

I am having trouble getting the data inside a DataTable into a DataGridView. I found code in Visual Basic and tried translating it into Visual C++.
String^ cninfo = "server=localhost; port=3306; username=test; password=Lalala123; database=turismo";
MySqlConnection^ cn = gcnew MySqlConnection;
MySqlDataAdapter^ cmdadp;
DataTable^ Table = gcnew DataTable;
String^ search = searchtxt->Text;
int^ rowposition = 0;
String^ id_searchQuery = ("select * from clientes where CLIENT_ID = #search;");
String^ name_searchQuery = ("select * from clientes where FIRST_NAME like '%#search%';");
String^ lastname_searchQuery = ("select * from clientes where LAST_NAME like '%#search%';");
String^ origin_searchQuery = ("select * from clientes where COUNTRY_ORIGIN like '%#search%';");
if (radid /*Radbutton for id_searchQuery*/->Checked == true) {
cn->ConnectionString = cninfo;
cn->Open();
//defining parameters for the search query
MySqlCommand^ searchCmd = gcnew MySqlCommand(id_searchQuery, cn);
searchCmd->CommandType = CommandType::Text;
searchCmd->Parameters->AddWithValue("#search", search);
//filling the DataTable with MySqlDataAdapter
cmdadp->Fill(Table);
//counting each DataTale line to enter it into a DataGridView with a while function
while (rowposition < Table->Rows->Count) {
DataRow^ MyDataRow = Table->Rows(rowposition);
}
cn->Close();
}
On (rowposition < Table->Rows->Count) { i get the "<" highlighted and i get
No operator "<" matches these commands
And on DataRow^ MyDataRow = Table->Rows(rowposition); i get highlighted "Table" and i get
call of an object of a handle type without apropriate operator () or conversion functions to pointer-to-function type

What happens if you just declare rowposition as an int rather than int^?
Table->Rows is of type DataRowCollection Class. Try accessing the row using the Item property array notation, e.g., Table->Rows[rowposition]
answered by #Phil Brubaker

Related

How to Generate CheckBoxes checked inside a DataGridView dynamically in C#?

There is a DataGridView that has a CheckBox Column. I'm generating the other columns of DataGridView from a table. It's working fine but now I'm trying some checkboxes to be checked using this code but it is not working. The code looks like
string query = "SELECT ID, Group_Name+' '+Phone_No as Info FROM Group_Info";
GenerateGridView(dataGridView1, query);
DataTable dt = GetTableData("SELECT Group_ID FROM tblGenerate");
foreach(DataRow rw in dt.Rows )
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)(row.Cells[0].Value );
if (Convert.ToInt32(row.Cells[1].Value) == Convert.ToInt32(rw["Group_ID"]))
{
chk.Value = chk.TrueValue;
}
}
}
How can we do this?
May you try this:
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
dataGridView1.Columns.Add(chk);
chk.HeaderText = "Check Data";
chk.Name = "chk";
dataGridView1.Rows[2].Cells[3].Value = true;

Retrieving MsAccess Data to ComboBox in VC++ form Application

Hi i am trying to retrieve data from Ms Access database in VC++. As i am a new to VC++, please help me.
Here is the code i have written so far.
System::Data::DataSet^ ds=gcnew System::Data::DataSet();
OleDbConnection ^ con=gcnew OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data source=dbmc.accdb; Persist Security Info=True");
OleDbCommand^ com =gcnew OleDbCommand();
OleDbDataReader^ myReader;
com->CommandText ="SELECT name FROM Table1";
com->Connection = con;
con->Open();
try
{
myReader=com->ExecuteReader();
while(myReader->Read())
{
String^ vName = myReader->GetString('name');
comboBox1->Items->Add(vName);
myReader->Close();
}
}
catch(Exception^ex)
{
MessageBox::Show(ex->Message);
}
When I run this program i get an error "Index Out of Bound".
The GetString() method takes an integer of the column number as a parameter, not the column name (see here for full documentation).
Change the line
String^ vName = myReader->GetString('name');
to
String^ vName = myReader->GetString(0);

How to set Spinner selection by value, using CursorAdapter

I have a spinner I'm populating from the database. I want to choose which item from the list is selected by default. I need to find out what item in the list (CursorAdapter) has the value "Default Away" and set that to the selected value.
Spinner away_team_spinner = (Spinner)findViewById(R.id.away_team_spinner);
DatabaseHelper db = new DatabaseHelper(this);
Cursor team_list = db.getTeams(p_game_level);
startManagingCursor(team_list);
String[] team_name = new String[]{colTeamName};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, team_list, team_name, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
away_team_spinner.setAdapter(adapter);
//// HERE IS WHERE MY ERRORS START ////
Log.i("NEW_GAME","Before set arrayadapter");
CursorAdapter adapter_choose = (CursorAdapter)away_team_spinner.getAdapter();
Log.i("NEW_GAME","Before set setSelection");
away_team_spinner.setSelection(adapter_choose.getPosition("Default Away"));
This is the "solution" I found by searching on this web site. However, I cannot use "getPosition" with CursorAdapter object. I tried ArrayAdapter, but then the line after "Before set arrayadapter" comment errors with "android.widget.SimpleCursorAdapter cannot be cast to android.widget.ArrayAdapter". What am I doing wrong? Thanks.
have you thought about running a for loop until you find the position then set the adapter position that way? ill draft up some code then test it, i'm doing something similar
and well this just did the trick, enjoy!
int cpos = 0;
for(int i = 0; i < simpleCursorAdapter.getCount(); i++){
cursor.moveToPosition(i);
String temp = cursor.getString((your column index, an int));
if ( temp.contentEquals(yourString)){
Log.d("TAG", "Found match");
cpos = i;
break;
}
}
spinner.setSelection(cpos);

AS3 Using a variable with Transform

I have a text field and a background and want to apply color using
myColorPicker. Either the text field or background can be selected using
radioGroup1. When either radio button is selected the trace statement
traces the variable obj2Clr exactly. However when I use that variable
with Transform, I can't apply color. If I hard code and use the actual
object then it works.
Can I not use a variable with Transform or is something else missing?
My code is below:
var radioGroup1:RadioButtonGroup = new RadioButtonGroup("selObj");
bkg_rb.label = "Background";
text_rb.label = "Text";
bkg_rb.group = radioGroup1;
text_rb.group = radioGroup1;
var obj2Clr;//which object to apply color to
radioGroup1.addEventListener(MouseEvent.CLICK, getObj);
function getObj(e:MouseEvent):void {
if (bkg_rb.selected == true) {
obj2Clr = "MovieClip(parent).design_mc.bkg_mc";
trace(obj2Clr);
} else if (text_rb.selected == true) {
obj2Clr = "MovieClip(parent).design_mc.info_txt";
trace(obj2Clr);
}
}
var colorTrans:ColorTransform = new ColorTransform();
var trans:Transform = new Transform(obj2Clr);
//var trans:Transform = new Transform(MovieClip(parent).design_mc.info_txt);
myColorPicker.addEventListener(ColorPickerEvent.CHANGE, changeColor);
function changeColor(event:ColorPickerEvent):void {
var myColor = "0x" + event.target.hexValue;
colorTrans.color = myColor;
trans.colorTransform = colorTrans;
trace("color selected is " + myColor);
}
Thanks for your help in advance:)
Debbie D
According to this code, obj2Clr is being initialized with a string literal?
For example, shouldn't this snippet:
if (bkg_rb.selected == true) {
obj2Clr = "MovieClip(parent).design_mc.bkg_mc";
trace(obj2Clr);
}
be:
if (bkg_rb.selected == true) {
obj2Clr = MovieClip(parent).design_mc.bkg_mc;
trace(obj2Clr);
}
?
Thanks, yes I thought I had to use a string literal with Transform because tracing out the variable without quotes that make it a literal resulted in [object MovieClip] and [object TextField].
So I removed the quotes and Transform still is not receiving the new Transform object. Yet when I hard code (which was commented out in the above example) everything is fine. Any other area I should check?
Debbie D :)

SubSonic SubSonic.SqlQuery & Dates

Does SubSonic.SqlQuery have a between/and for date ranges? If not, what would be the best way to get a range.
Try something like this:
SqlQuery query = new SqlQuery().From("Table")
.WhereExpression("Column")
.IsBetweenAnd("1/1/2008", "12/31/2008");
DataSet dataSet = query.ExecuteDataSet(); // Or whatever output you need
Another way to query with SubSonic.
TableCollection data = new TableCollection();
Query q = Table.CreateQuery()
.BETWEEN_AND("Column", "1/1/2008", "12/31/2008");
data.LoadAndCloseReader(q.ExecuteReader());
// loop through collection
Combined Northwind answer:
SqlQuery query = new SqlQuery().From("Orders")
.WhereExpression("OrderDate")
.IsBetweenAnd("1996-07-02", "1996-07-08");
DataSet dataSet = query.ExecuteDataSet(); // Or whatever output you need
#region PresentResultsReplaceResponseWriteWithConsole.WriteLineForConsoleApp
DataTable dt = dataSet.Tables[0];
Response.Write("<table>");
foreach ( DataRow dr in dt.Rows )
{
Response.Write("<tr>");
for (int i = 0; i < dt.Columns.Count; i++)
{
Response.Write("<td>");
Response.Write(dr[i].ToString() + " ");
Response.Write("<td>");
} //eof for
Response.Write("</br>");
Response.Write("</tr>");
}
Response.Write("<table>");
#endregion PresentResultsReplaceResponseWriteWithConsole.WriteLineForConsoleApp

Resources