C# :: Read SQL Table To Datagrid
Feb 9, 2014
I have been struggling to implement a method for reading data from SQL server to my C# program. In the C# program I prepared a DataGrid (not a DataGridView) for the data to be shown in it using a button "Load Table". Now I just want to read a table which has 4 different columns which I also added to the DataGrid.
I have a DataAccessDB class and this is the code for establishing a connection:
const string DB_CONNECTION = @"It is known to me";
SqlConnection con;
SqlCommand cmd;
Controller myController;
Then, there is a code to reference controller:
public DataAccessDB(Controller refController) {
con = new SqlConnection(DB_CONNECTION);
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
myController = refController;
}
And this is the code for a List which I am going to use for filling the data to a DataGrid:
public List<ProductionStock> GetProductionStockFromDB() {
int productionStockId, amount;
decimal price;
string name, descript;
[code]....
And now the main part - how to read the data when I press a LoadTable button. The point here is that I already defined connection strings here as you can see, and in the method itself I guess I should not repeat it. The tutorials I have seen they do it everything in that one button.
View 10 Replies
ADVERTISEMENT
Mar 30, 2014
i have the following code
protected void Page_Load(object sender, EventArgs e)
{
List<Product> Products = DbConnectivity.LoadProduct();
updateTable(Products);
[Code].....
the code works and produce a table and fills the grid, what im trying to do is get this information into a DataGrid, so i can get rows selected by user. ive tried many ways ie convert the list into a datatable and display but produces errors, ive done the following,
pageLoad....{
FillDataGrid();
DataGridProduct1.DataBind();
}
[Code].....
this populates, but couldnt seperate the SQL connection apart, and it makes no use of the class product
View 6 Replies
View Related
Nov 13, 2014
How can i read particular record in a table quickly via VC++ from MYSQL?
I have a table like
Code:
Database name: test & Table name : Profilemaster
+-----+--------+
| PID | PNAME |
+-----+--------+
| 1 | APPLE1 |
| 2 | APPLE2 |
| 3 | APPLE3 |
| 4 | APPLE4 |
| 5 | APPLE5 |
+-----+--------+
My code is here, I was read all profilename from MYSQL database and load it to a combobox.
How can i read a particular record and store it to combobox?
Code:
void MainScreen::OnreadProfileName() {
// TODO: Add your control notification handler code here
CDatabase database;
CString SqlString;
CString sDsn;
CString pname;
[code].....
How can i execute this query and get the following result
SqlString = "SELECT PNAME FROM PROFILEMASTER WHERE PID =1;";
Code:
+-----+--------+
| PID | PNAME |
+-----+--------+
| 1 | APPLE1 |
+-----+--------+
View 4 Replies
View Related
Nov 14, 2014
My table:
Code:
Database name: test & Table name : Profilemaster
+-----+--------+
| PID | PNAME |
+-----+--------+
| 1 | APPLE1 |
| 2 | APPLE2 |
| 3 | APPLE3 |
| 4 | APPLE4 |
| 5 | APPLE5 |
| 6 | APPLE6 |
| 7 | APPLE7 |
| 8 | APPLE8 |
| 9 | APPLE9 |
| 10| APPLE10 |
+-----+--------+
I like to read the table and get the all PNAME into the combo box.
Using the below code i can read the table, but while loop takes 2 seconds to read 10 records in the Profilemaster table.
How can i reduce the reading time?
My Code is
void MainScreen::OnreadProfileName() {
CDatabase database;
CString SqlString;
CString sDsn;
CString pname;
sDsn.Format("Driver={MySQL ODBC 5.2 ANSI Driver};Server=localhost;Database=test;User=root;Password=client;Option=4;");
[Code] ....
Is any other way to reduce the query execution time?
View 6 Replies
View Related
Nov 19, 2013
I am having an issue with my sort function. This is one part of the Hash table program. The main issue is that I am trying to sort the pointer table after all of the values have been entered. The ptr_sort function is not a class function and so I am therefore unable to use the class variables psize and pTable. Is there another way I should be trying this? it is a Vector should I use the sort() function from the vector class in STL?
#include "/home/onyuksel/courses/340/progs/13f/p9/util9.h"
#include "/home/onyuksel/courses/340/progs/13f/p9/hTable.h"
#ifndef H_TABLE1
#define H_TABLE1
void ptr_sort ( );
[Code] ....
View 1 Replies
View Related
Jan 25, 2013
If a user enters a string of boolean algebra it will ouput the table.
I have input parsing, cycling through the combinations, and outputing working. However once i parse the input I am not sure what to do with it. I have thought of having it write the parsed input to a new file as a function and then use that function, but that seems bad.
How to dynamically create the function, how to implement it.
BTW This is a console function, if that changes anything.
View 2 Replies
View Related
Dec 15, 2014
I have written an SQL statement that would:
1) Count the number of rows in the table booking that are open and where the booking.postcode is "MK",
2) Take a note of the plot (in booking.plot_id), and then update the table plot.jobs with the value count.
For example running the SQL query when booking table has the following rows:
Would see the following highlighted values in plot.jobs being updated to 1:
I managed to resolve it with this code so far (note I am using Connector/Net):
public void RefreshPlot(){
string query =
"SELECT Count(*) AS count, plot_id FROM booking WHERE postcode='MK' AND status='open' GROUP BY plot_id";
var cmd = new MySqlCommand(query, _connection);
var da = new MySqlDataAdapter(cmd);
var dtCounts = new DataTable();
da.Fill(dtCounts);
[code]....
Currently, my code only checks for existing rows in the booking table and updates the plot table. However if the row gets deleted in the booking, then the changes are not reflected in the plot table.
Example: If we delete the row with plot.id=1 and plot.plot_id=4, then booking.plot_id should go back to being 0, if it was initially 1. At the moment, it doesn't. How would I update my SQL statement to better reflect this? In a sense, it should check for "non-existent" rows, i.e. the impact that the row plot.plot_id=4 & plot.id=1 has on booking.plot_id when deleted?
View 6 Replies
View Related
Mar 25, 2013
I have a C# .NET Application which get data from QuickBooks via the ODBC Driver and save the result to C# data table. So, I want to transfer this data table to a mysql table on my own server. That's the code I use:
using System.IO;
using MySql.Data.MySqlClient;
//Add mysql dll on the .NET Tab in Project's references
string connStr = "DSN=QBTest;";
string myServerAddress = "192.168.0.243";
string myDataBase = "CostTest";
[Code] ....
View 2 Replies
View Related
Apr 3, 2015
I am trying to get a datagrid working in my program with populating a simple set of data. Which i can then expand on and bring in data from a database.
But even this little simple test datagrid does not seems to be working.
XAML Code
<DataGrid Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2" Margin="10" Name="DataGrid_1" ItemsSource="{Binding DataGridData}"></DataGrid>
<Button Grid.Row="6" Grid.Column="2" Margin="10" FontSize="12" Command="{Binding Path=GetDataGridData_Command}">Get Data</Button>
[Code].....
View 3 Replies
View Related
Nov 20, 2014
I want to print the current grid values while clicking the print option ..
View 7 Replies
View Related
Dec 15, 2014
I'm trying to create custom WPF control, similar to DataGrid. DataGrid was great as a base class for a different custom control I needed, because I could do something like this:
<shared:MyDataGrid ItemsSource="{Binding Persons}" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Width="4*" />
<DataGridTextColumn Binding="{Binding Phone}" Width="3*" />
<DataGridTextColumn Binding="{Binding Email}" Width="3*" />
</DataGrid.Columns>
</shared:MyDataGrid>
where Person class would be like this:
public class Person
{
public string Name {get; set;}
public string Phone {get; set;}
public string Email {get; set;}
}
... and I can reuse it elsewhere with collections of different type, binding its properties to columns of different width and style.
What I need now is something similar, but differs in a way, that DataGrid is no longer good fit for the base class.
What I'm interested in is: how does DataGrid "tell" the columns in Columns collection, to have the type of ItemsSource bound collection as a DataContext? How does the Name property of Person class become available to the column in DataGrid.Columns?
View 3 Replies
View Related
Mar 29, 2014
im trying to populate a datagrid, i have a datbase class
public static List<Product> LoadProduct()
{
....................
}
i have a product class
gettor & settors
public Product(int id, string Name, double Price, int Quantity)
{
productId = id;
productName = Name;
productUnitPrice = Price;
productQuantity = Quantity;
}
ive added the HTML code to set up the datagrid, which i can see via design tab, but filling it up is harder.
i uderstand it needs a
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
sqlDataAdapter1.Fill();// where it require a dataset or datatable
DataGridProduct.DataBind();
as it requires a datatable i converted the list to a datatable when i run it it says null, im not sure where im going wrong, i noe the list works as i tried populating on a table.
public static DataTable ToDataTable<T>(List<T> items)
{
}
View 1 Replies
View Related
Mar 8, 2015
I am a bit stumped with trying to write some code to get the tableadapter/binding source to "update" a current 'user' to the table.
I have the insert/delete methods working fine, but it's the updating that has got me screwed up.
Example of insert.
try
{
personTableAdapter.Insert(tFirstName.Text, tSurname.Text, Convert.ToDateTime(tDoB.Text), tPhone.Text, tAdd1.Text, tAdd2.Text, tSuburb.Text, tState.Text, tPostcode.Text, TeacherType.Text);
teacherTableAdapter.Insert(Convert.ToInt32(tTID1.Text), tRegNo.Text, tPassword.Text);
[Code]....
I also tried to do the updated string/original string with the tableadapter.update, but that didn't work either.
View 7 Replies
View Related
May 21, 2014
I have a datagrid, which has a column called Status. In the database, the field is a text field that can have the values up, down, or danger.
I needed to display the status as a "light" as per client request, so I have a StatusToImageConverter class that receives the status, and returns the proper image to display in the datagrid column. Works fine.
Now the issue I am having... I have a combo box above the datagrid for the user to filter the results if desired. The filtering works fine, but the newly displayed devices do not have their status light. In the Status column, nothing appears after the user filters. I think it may have to do with the fact that I am not re-initializing the datagrid, but just refreshing it? That's just my educated guess.
XAML:
In the UserControl Resources:
<local:StatusToImageConverter x:Key="ImageConverter" />
In the DataGrid Control:
<DataGridTemplateColumn Header="Status" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
[Code].....
View 5 Replies
View Related
Oct 8, 2012
how to create e button in Datagrid for each row with the function to Expand/Collapse when it is clicked.
I want to show some data for the row where the button is clicked.
View 5 Replies
View Related
Jul 6, 2014
I want to be able to keep on adding a row to my datagrid every time i click a button, the row will hold the text of the text box and the combobox in its individual cells, is there a way to do this without inserting a ridiculous amount of code?
Attached image(s)
View 1 Replies
View Related
Jul 19, 2014
I have made a datagrid in a WPF and added a button to add rows, here is the code for that:
this.itemListDataGrid.Items.Add(new TextBox());
Also, I have already got 3 colums in the datagrid pre-added by me, so when the rows are added I click on one to edit it and it comes up with error edit item not allowed.
View 7 Replies
View Related
Jun 5, 2012
I have the following code in the cellmouse event to get information from a selected row.
private void dt_Grid_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
datagrid.Rows[0][0] = dt_Grid.Rows[e.RowIndex].Cells[0].Value.ToString();
datagrid.Rows[0][1] = dt_Grid.Rows[e.RowIndex].Cells[1].Value.ToString();
datagrid.Rows[0][2] = dt_Grid.Rows[e.RowIndex].Cells[2].Value.ToString();
[Code]...
but when i select a collumn, i recive an error because i'm selecting a cell with the mouse and i dont have code for that selection. My question is how can i put an "if" on the beguining of the event so that he can determine if it's a header or not.
View 1 Replies
View Related
Apr 17, 2012
How do I order the value of the selected cell in a datagrid to textbox in another form
View 1 Replies
View Related
May 7, 2014
How can I update my dataGrid when a user makes a selection in the combobox?
Here's my ComboBox:
<ComboBox HorizontalAlignment="Left"
Margin="125,110,0,0"
VerticalAlignment="Top"
[Code]....
View 5 Replies
View Related
May 7, 2012
In my WPF application I created a LINQ to SQL entities class. Then I created a partial class and there add code to my entity class. Below is the code of it:
namespace Mynamespace {
partial class rts_index {
#region Fields
/// <summary>
/// Status:
/// "Selected" - true, "Unselected" - false.
/// </summary>
private bool _is_selected;
[code]....
Then I executed LINQ to SQL. Works perfectly well. Then I bound WPF Datagrid radiobutton column to is_selected property of the partial class. Below is XAML code:
.
.
.
<Window.Resources>
<CollectionViewSource x:Key="rts_indexViewSource" d:DesignSource="{d:DesignInstance my:rts_index, CreateList=True}" />
</Window.Resources>
[code]....
And here I have the problem. When I'm changing the radiobutton status from Unchecked to Checked it doesn't change the value of is_selected property from false to true in the partial class. I don't know why.
View 1 Replies
View Related
Apr 25, 2014
How do I bind a DATE column in a DataGridView Control to a DatePicker control (using C#)? I already have the DataGridView control bound to a database stored procedure which takes a DATE value as a parameter and selects joining table based on the results.
View 7 Replies
View Related
Dec 8, 2013
Is possible read text form keyboard using read() function? or which is best way in ansi creplace input command form basic language. for numbers and text...
View 3 Replies
View Related
May 15, 2013
I want to read the contents of a file block (512 bytes) by block using low I/O read statements. Each record is 64 bytes long and has a pre-defined structure. The first 4 bytes are an unsigned integer; the next 20 bytes are ascii text, etc.
I have a buffer which I can access with buf[0] to buf[63] to read the first record and then buf[64] to buf[127] for the second, etc. However, I was wondering how to map a record so that I can refer to an integer as an integer and a float as float, etc. I can't create a struct and move the 64 bytes to it, as I will have alllignment/padding problems.
What is the standard way to deal with records in C?
View 5 Replies
View Related
Apr 18, 2013
I have a problem with this function that adds a new value to the hash table or updates its data if the value already exists:
Code:
int tab_add(tab_disp *td, const object *value) the function should return:
TABDISP_OK if the value is correctly added, or TABDISP_ERROR if memory error, or TABDISP_INVALID if pointer td = NULL
The type of structure I am using is the following:
Code:
typedef struct {
char key[25];
char value[100];
} object;
[code]....
View 2 Replies
View Related
Aug 5, 2013
I'd like to get a a field name "eventDateTime" from a DB table and its storage is datetime year to fraction (3) . I have created a function return results like this but i put its type as a int:
int Status::GetEventFromSQL(){
int j = 0;
int eventTime = 0;
MYSQL_ROW myROW;
MYSQL_RES *myRES = NULL;
[code]....
"eventTime " is also declared in another sheet .h as an int. In execution i have a big and negative number ; I know that's because of the int type but i'm bigenner in C++...
View 2 Replies
View Related