|
hi
i did this:
in my Access db table, i've rows of data. each row have columns (i've a column representing ID number and other columns for other data). in VS, i've a textbox for the user to enter an ID number and then press OK button.
how to do this:
the pressing of OK button should retrieve the entire row of data belonging to THAT ID number keyed in and then displaying the data in a form in VS.
do i have to do anything regarding filtering, parameters or setting criteria?
can i have some sample codes/examples?
thanks very much!
|
|
|
|
|
You send a query to your Access Database to request the row. Something like this:
OleDbCommand cmd = new OleDbCommand(myConnection,
"SELECT id, dataColumn1, dataColumn2, etc FROM myTable WHERE id = ?")
cmd.Parameters.Add(id);
OleDbDataReader reader = cmd.ExecuteReader();
int id = reader.GetInt32(0);
string myValue = reaser.GetString(1);
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
sorry for my ignorance but how do i actually go about to send a query?
|
|
|
|
|
That is what the code I gave you does. It sends the query to the database and retrieves the result. Here is again, but annotated more fully.
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
myConnection.Open();
OleDbCommand cmd = new OleDbCommand(myConnection,
"SELECT id, dataColumn1, dataColumn2, etc FROM myTable WHERE id = ?");
cmd.Parameters.Add(id);
OleDbDataReader reader = cmd.ExecuteReader();
int id = reader.GetInt32(0);
string myValue = reaser.GetString(1);
myConnection.Close();
Also, the MSDN documentation will explain each of these classes and methods with further examples.
* DISCLAIMER: I typed this by hand without running it through a compiler. There may be some small errors.
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
oh thanks so much for a more detailed description! it's more clear to me what those codes mean now. =)
but what i meant was what actually are the steps to send a query to my database? what am i supposed to click on to enter those codes that you have shown me?
>sorry im new to database stuff and i really need some help in making things work.. and seriously, i learn better this way, through step by step guidance and hands-on experience.. nevertheless, i'll be reading up on the relevant links that may be helpful..
thanks again! =)
|
|
|
|
|
nidhelp wrote:
but what i meant was what actually are the steps to send a query to my database?
The steps are those I've shown you above.
nidhelp wrote:
what am i supposed to click on to enter those codes that you have shown me?
Umm... The text editor. Then you start typing. (I know that sounds a stupid answer, but there is nothing to click on. No magic buttons to press. You really have to do the work).
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
SORRY im still stuck...i cant figure out where to type the codes. which section of my program should i type?
THANKS!
|
|
|
|
|
There are many ways to do this. One of the correct ways is to create a DAL (Data Abstraction Layer) in your application. This is really just a class, or set of classes (if your application is big) that communicates with the database for you. It then returns the data to what ever part of the application needed it.
e.g.
class Dal
{
private string connectionString;
public Dal()
{
connectionString = ConfigurationSettings.AppSettings["connectionString"];
}
private SqlConnection Connection
{
get
{
return new SqlConnection(connectionString);
}
}
public Hashtable GetMyData()
{
Hashtable result = new Hashtable();
for(int i=0; i<myDataReader.FieldCount;i++)
{
string columnName = myDataReader.GetName(i);
object columnValue = myDataReader.GetObject(i);
result[columnName] = columnValue;
}
return result
}
}
Then somewhere in the code that needs the data from the database you can write something like this:
Dal dal = new Dal();
Hashtable data = dal.GetMyData();
Does this help?
My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
|
|
|
|
|
Hey! I've done something similar to this and i know just how you feel. My suggestion is that you separate your queries (data gathering from database) from you UI form. Create another form which would contain your connection string as well as all your queries then pass it to your main form.
Try this:
public class DS_Company<br />
{<br />
OdbcConnection conn = new OdbcConnection("ConnectionString");<br />
string strSQL;<br />
<br />
public DS_Company()<br />
{<br />
conn.Open();
}<br />
<br />
public DataSet GetID(string strID)<br />
{<br />
strSQL = " SELECT ";
strSQL += " * ";
strSQL += " FROM Table";
strSQL += " WHERE IDnumber='"+ strID +"'";
<br />
DataSet ds = new DataSet();
OdbcDataAdapter adpDS = new OdbcDataAdapter(strSQL,conn); <br />
adpDS.Fill(ds);
return ds;
}<br />
}<code><br />
<br />
hope that helps <br />
<br />
Kampai!!!
|
|
|
|
|
Well to tell you the truth im a beginner too but i'l try my best to be of assistance. Go ahead and post your code or better yet email me your form. I'l see what i can cook-up
Kampai!!!
|
|
|
|
|
OH YES!!!!!!! I managed to solve it already!
|
|
|
|
|
oh no! my problem's not solved yet...
im doing parameterised query. i managed to load my data and correctly inside the textbox controls which are placed on the same page as where i've placed my textbox and Go button.
if this is not clear, i mean i managed to do this> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbwlkwalkthroughdisplayingdatainwindowsformusingparameterizedquery.asp
however, what i want to do is, the user types a criterior in the textbox and press the Go button which are in form1. the Go button sends query to Access db and loads the data inside the textbox controls in form2.
thanks
|
|
|
|
|
|
HI
Im trying to create a c#.net windows form program that keeps track of a series of sports results. I want to be able to enter all the teams into a list box at the start of the program and then store this information in a Dataset. This Dataset is then saved as an xml file. After the program has been restarted i want the listbox to be populated with the data saved in the xml file.
Am I able to bind the listbox to a table in the dataset? If so how do i load data for the next entry in the listbox and so on?
thanks
Rich
|
|
|
|
|
HI
Im trying to create a program that keeps track of a series of sports results. I want to be able to enter all the teams into a list box at the start of the program and then store this information in a Dataset. This Dataset is then saved as an xml file. After the program has been restarted i want the listbox to be populated with the data saved in the xml file.
Am I able to bind the listbox to a table in the dataset? If so how do i load data for the next entry in the listbox and so on?
thanks
Rich
|
|
|
|
|
I know there are so many question regarding this NullReferenceException...
but still i couldn't find any solution for my prob...
i've got a runtime error...
it says that...
"An unhandled exception of type 'System.NullReferenceException' occurred in OraGraphing.exe
Additional information: Object reference not set to an instance of an object."
this is my code... the error is at price...
<br />
public int counter;<br />
public sting[] price;<br />
<br />
private void Form1_Load(object sender, System.EventArgs e)<br />
{<br />
OracleConnection oraConn = new OracleConnection("Data Source=quotes; user id=me; password=you;");<br />
OracleCommand oraCmd = new OracleCommand("SELECT isin, to_char(trading_date,'dd/mm/yy hh24:mi:ss'), last_done_price FROM realtime_tracking",oraConn);<br />
oraConn.Open();<br />
OracleDataReader oraDR = oraCmd.ExecuteReader();<br />
<br />
counter = 0;<br />
while(oraDR.Read())<br />
{ <br />
try<br />
{<br />
price[counter] = oraDR["last_done_price"].ToString();
}<br />
catch (NullReferenceException nre)<br />
{<br />
MessageBox.Show(nre.Message);<br />
}<br />
counter++;<br />
};<br />
<br />
oraDR.Close();<br />
oraConn.Close();<br />
}<br />
I think that error because of i didnt declare how large the array is... but how? since i didnt know how large is the data inside that database...
-----------------------------------------------
=^_^= I'm a noob whose trying to learn... =^_^=
|
|
|
|
|
|
yup..
its true.. when i put how many elements.. it runs correctly..
but.. i want to make it dynamically,
since the data in the database keeps updating/increasing/decreasing..
so.. basically i dont know the number of elements..
------------------------------------------------------
=^_^= Thank you to those who answer my question =^_^=
|
|
|
|
|
|
|
Use a list instead of an array, like an ArrayList, then you don't have to know the number of items to create it.
---
b { font-weight: normal; }
|
|
|
|
|
<br />
price = new string[oraDR.Count]; <br />
while(oraDR.Read())<br />
{ <br />
try<br />
{<br />
price[counter] = oraDR["last_done_price"].ToString();
}<br />
catch (NullReferenceException nre)<br />
{<br />
MessageBox.Show(nre.Message);<br />
}<br />
No hurries, no worries.
|
|
|
|
|
sorry 'bout themany replies, CP was working against me
No hurries, no worries.
|
|
|
|
|
I want to Enumerate Users on Remote Machine with username and password. I am using NetEnumUser function in (Advapi32.dll)/ this function enumerates user with windows 2000 but when runs on windows xp it returns access denied. I am searching for a function that will authenticate my credentials and get the remote machine users.
Thanks in Advance
|
|
|
|
|
hello friends,
i am developing an application, which needs to transfer the data from one computer to another with the help of socket programming.
i want to make the server in the listening mode on port 80, so that whenever client send the connection request, it will be connected on port 80.
please tell me whether it is possible or not to put the application on the listening mode on port 80.
i tried it using system.net namespaces's SocketObj.Bind method using port 80 but it gives the error that
"Only one usage of each socket address (protocol/network address/port) is normally permitted".
this will work fine for other ports. but for other ports i have the problem that may be that port will be blocked by the company where this project is implemented.
please help me anyway if u can!
Thanks to all and codeproject too.
Gagan Deep Singla
Chandigarh - INDIA
|
|
|
|