|
How do bind a data using DataReader?
|
|
|
|
|
Hi,
First build a connection string and then initialise the command and reader object as
Write this in the event when you want it to be done:(i have used MSAccess as database)
OleDbConnection conn=new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\new1.mdb");
conn.Open();
OleDbCommand cmd=new OleDbCommand("select * from Table1",conn);
OleDbDataReader rdr=cmd.ExecuteReader();
rdr.Read();
DataGrid1.DataSource=rdr;
DataGrid1.DataBind();
This is a simple example..
YOu try out for more samples..
Feel free to ask anything regarding this..
|
|
|
|
|
hi
try this:
1. open connection
2. SqlCommand objSqlCommand = new SqlCommand();
3. SqlDataReader objSqlDataReader;
objSqlDataReader = objSqlCommand.ExecuteReader();
objSqlDataReader.Read();
grid.DataSource = objSqlDataReader;
grid.DataBind();
objSqlDataReader.Close();
3. close connection
|
|
|
|
|
Hello everyone,
Greetings from Brazil! As shown in the code below, is it possible for me to add the new objects for tipoDT and sementesDT without having to do it one-by-one? Like, for example, getting the values automatically from the tables?.... How would I do that? The sementesDT table is quite large and would take me forever to add the new objects one-by-one! Here's the code:
public frmBA()
{
tipoDT = new DataTable("tabTipoSemente");
tipoDT.Columns.Add("CodTipo", typeof(int));
tipoDT.Columns.Add("Tipo", typeof(string));
tipoDT.Rows.Add(new object[] { 0, "Nocivas Probidas" });
tipoDT.Rows.Add(new object[] { 1, "Nocivas Toleradas" });
tipoDT.Rows.Add(new object[] { 2, "Sementes Silvestres" });
sementesDT = new DataTable("tabSementes");
sementesDT.Columns.Add("CodSemente", typeof(int));
sementesDT.Columns.Add("CodTipo", typeof(int));
sementesDT.Columns.Add("Semente", typeof(string));
sementesDT.Rows.Add(new object[] { 0, 0, "SubCat0-Cat0" });
sementesDT.Rows.Add(new object[] { 1, 0, "SubCat1-Cat0" });
sementesDT.Rows.Add(new object[] { 2, 0, "SubCat2-Cat0" });
sementesDT.Rows.Add(new object[] { 3, 1, "SubCat3-Cat1" });
sementesDT.Rows.Add(new object[] { 4, 1, "SubCat4-Cat1" });
sementesDT.Rows.Add(new object[] { 5, 1, "SubCat5-Cat1" });
sementesDT.Rows.Add(new object[] { 6, 2, "SubCat6-Cat2" });
sementesDT.Rows.Add(new object[] { 7, 2, "SubCat7-Cat2" });
sementesDT.Rows.Add(new object[] { 8, 2, "SubCat8-Cat2" });
InitializeComponent();
tipoBS = new BindingSource();
tipoBS.DataSource = tipoDT;
TipoComboBoxColumn.DataSource = tipoBS;
TipoComboBoxColumn.DisplayMember = "Tipo";
TipoComboBoxColumn.ValueMember = "CodTipo";
unfilteredSementesBS = new BindingSource();
DataView undv = new DataView(sementesDT);
unfilteredSementesBS.DataSource = undv;
EspecieComboBoxColumn.DataSource = unfilteredSementesBS;
EspecieComboBoxColumn.DisplayMember = "Semente";
EspecieComboBoxColumn.ValueMember = "CodTipo";
filteredSementesBS = new BindingSource();
DataView dv = new DataView(sementesDT);
filteredSementesBS.DataSource = dv;
}
Thank you very much for your attention, time and help and I'm looking forward to your reply.
Best regards,
JC Carmo
|
|
|
|
|
JC Carmo wrote: Like, for example, getting the values automatically from the tables?....
What tables ? If you mean in a database, then yes, that's what a dataset is really for.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I mean the tabSementes table that is referenced in the code. How do I connect it to a DataSet ? Thanks for your reply.
JC.
|
|
|
|
|
A DataSet is a collection of DataTables, I'd assume you can create a datatable and add it to the Tables collection of a DataSet.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Hi Christian,
It's me again. I don't mean to be a drag, sorry about it, but I know how to connect to a dataSet in events such as form-load, but in this particular case, I'm a little lost, since the code I posted is part of teh code that filter values in comboboxes in a datagridview. Thanks a lot.
JC.
|
|
|
|
|
If you're copying an entire table, you can probably call it's Clone() method to create a copy and stuff it into a dataset. I'm guessing here tho. Why are you manipulating datasets and tables like this ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Dear All,,
Can U Help me to Generate the XML document from Excel Worksheet.Excel 2003 professional Edition have an option like XML in Data Menu..I have Excel 2003 Standard Edition...
Please Help me to get the Solution
Thanks n Regards,
Ramya.R
-- modified at 0:01 Friday 24th February, 2006
|
|
|
|
|
You can use dataset.GetXml method to get the xml
private const string xcel_CONN_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;";
internal DataSet GetExcelDataToDataSet(string filepath, string sheetname)
{
OleDbConnection cn = null;
OleDbDataAdapter da = null;
DataSet ds = null;
try
{
cn = new OleDbConnection(string.Format(xcel_CONN_STRING, filepath));
cn.Open();
da = new OleDbDataAdapter(string.Format(xcel_sql, sheetname), cn);
ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if(cn != null)
{
cn.Close();
cn.Dispose();
}
}
}
|
|
|
|
|
Hello
How to set DataGridViewButtonColumn 's Enabled ??
best wishes
hello
|
|
|
|
|
I m making an applicaiton that manages printing usin WMI calls. my appication looks like the queue similar to windows queu. There are 2 listboxes. 1 is showing printer name and the other is showing jobs associated with the selected printer in first listview.
on the selectedindex changed of listview1 i have filled listview2 the code for listview1 selected index changed is as follows:
string[] details = new string[12];
ListViewItem lv;
int i=0;
string a="";
Decimal size;
string submited_date;
try
{
a = this.listView1.SelectedItems[0].Text;
printername = this.listView1.SelectedItems[0].Text;
printerindex=this.listView1.SelectedItems[0].Index;
}
catch (Exception){}
try
{
this.listView2.Items.Clear();
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_PrintJob");
//listviewcolumes();
foreach (ManagementObject queryObj in searcher.Get())
{
System.String jobName = queryObj.Properties["Name"].Value.ToString();
char[] splitAr = new char[1];
splitAr[0] = Convert.ToChar(",");
string prntname = jobName.Split(splitAr)[0];
size = Convert.ToDecimal(queryObj["Size"].ToString());
string year = "";
string month = "";
string dates = "";
string hours = "";
string min = "";
string sec = "";
year = queryObj["TimeSubmitted"].ToString().Substring(0,4);
month = queryObj["TimeSubmitted"].ToString().Substring(4,2);
dates = queryObj["TimeSubmitted"].ToString().Substring(6,2);
hours = queryObj["TimeSubmitted"].ToString().Substring(8,2);
min = queryObj["TimeSubmitted"].ToString().Substring(10,2);
sec = queryObj["TimeSubmitted"].ToString().Substring(11,2);
//for (int x=0;x
|
|
|
|
|
Hi all,
I am developing a window application and have developed a menu. but i have to
add icons to menu items. pls tell me how this can be done. i am using c#.
Praveen.
|
|
|
|
|
|
You have answered your own question, actually.
If you create a boolean property inside a user control and then add the user control into a form (should be visible in the designer mode), then you can see the property in the Property Page of VS.NET editor.
There has to be more to life than just this
|
|
|
|
|
Hi,
I am a newbie to C# and Im trying to redirect standard input, output and error of a console program written in C (MS VC 6.0) to a textbox on a form. The code for the redirecting looks like this:
private System.IO.StreamWriter c_StreamInput = null;
private System.IO.StreamReader c_StreamOutput = null;
private Thread c_ThreadRead = null;
private Process c_Process = null;
private void ReadStdOutputThreadProc()
{
try
{
string str = c_StreamOutput.ReadLine();
while(str != null)
{
txtboxCNF.AppendText(str+"\r\n");
Thread.Sleep(100);
str = c_StreamOutput.ReadLine();
}
}
catch(Exception) {}
}
private void btnStart_Click(object sender, System.EventArgs e)
{
if(c_Process == null)
{
c_Process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("console.exe");
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
c_Process.StartInfo = psi;
c_Process.Start();
c_StreamInput = c_Process.StandardInput;
c_StreamOutput = c_Process.StandardOutput;
c_StreamInput.AutoFlush = true;
c_ThreadRead = new Thread(new ThreadStart(ReadStdOutputThreadProc));
c_ThreadRead.Start();
}
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (c_ThreadRead != null)
{
c_ThreadRead.Abort();
c_ThreadRead.Join();
}
if(c_Process != null && !c_Process.HasExited)
c_Process.Kill();
}
The console application that I am trying to run (console.exe) is a simple C program that prints a line text and then calls the getchar() function. The code is basically this:
#include "stdafx.h"
#include
int main(int argc, char* argv[])
{
printf ("Hello World !!\n");
int ch = getchar();
printf("Character = %s\n", ch);
return 0;
}
I have tried to run the C program without the getchar() function and it works. The problem arises when I use the getchar() function. I have tried getch() and gets() version without any success. I am not sure if I have to do anything special for showing output when using such functions.
Any help/pointers in this direction will be greately appreciated.
Thanks in advance.
Dribble
|
|
|
|
|
Ok should be a simple question as I didn't do research at all. (I'm tired, and been working all 15 hours on a coding project.) I would just like to know if there are any differences as far as connectors, queries and other coding related statements from SQL to MySQL. I am currently using MySQL to test the program, am I going to run into problems when making the final switch to SQL?
Thanks for all the help!
|
|
|
|
|
Hello,
If i am not mistaken, most probably you are asking about differece of queries between these two database engines.
If so, then here is my opinion.
The queries of MySQL is really different than SQL queries. Only very common queries will be same like, SELECT * FROM tableName, UPDATE tableName set ... etc., other than that, SQL queries are less forgiving than MySQL. In mySQL, joining table (inner join) , no need to mention in queries, like,
SELECT tableA.fieldA, tableB.fieldC from tableA,tableB WHERE tableA.id = 5 AND tableA.foriegnKey = tableB.primaryKey
But in SQL, you will have to use InnerJoin , ON etc phrase .
I know about parameters in SQL is @variable, in Access is , variable = ? , I dont know if mySQL has any parameter syntax to use in C#, as I used mySQL with PHP, not with anything else.
|
|
|
|
|
Thank you, usually I just use SQL, this time I am programming from home where I don't have access to a SQL server. Again, thanks for the quick response.
|
|
|
|
|
|
I understand, that this is simple question for peoples who work with C# some time. But there is the question: I create Windows forms dll in C# and I have to store bmp jpg files in this dll - not located as image in disk, but included in one dll file. Embedded resources or something? Please give me a tip.
|
|
|
|
|
|
Thanks - it was very fast
|
|
|
|
|
Hey howzit?
I have a Panel that displays 3 different photos as BackGroundImage's. One is 300dpi, 150dpi and 72dpi. Each photo causes the Panel to autosize.
I am using this as a zoom mechanism. So when the user clicks zoom, the image changes from 72dpi to 150dpi and 150dpi to 300dpi and that works fine, BUT i want the the scrollpositions of the form to change so that the area where the cursor is located on the image, is displayed in the middle. Similar to how we zoom to a specific spot in MS Word, i have used the code below but it doesn't seem to be very acurate, perhaps there is a way to achieve this specific position zoom in another way?
form.AutoScrollPosition = Cursor.Position;
Thanks!
|
|
|
|
|