Link to home
Start Free TrialLog in
Avatar of TSTechNA
TSTechNA

asked on

working con binding combobox

I'm a beginner C# programmer, I want to read in a text box a 'PartNo' that PartNo, i'm gonna look it for in a database and if it exists, bring information to a label (per example, description of this partno), and if it not exists, bring a  message box saying, "PartNo Not found",
How can i do this ? one idea, is by code.  make a sql connection, then with the command such SELECT * FROM Inventory bring data, to where ? a data adapter or something ?
then, how i can access to it ? then something like lbldescription.text= database.column.value or something, this is what i need to know, knowing how it works, practicing y i can so much things,
i appreciate your help
Avatar of jdavistx
jdavistx

If you simply wanting to view the data in your database, then there are much simpler alternatives than creating a C# app.

One such alternative is Crystal Reports.  You can use Crystal to create simple forms that you can format, edit and save the returned data from your database.

If you want to go the C# route, then there are several ways to accomplish your goal. Two of which would be:
- Bind your controls to your database fields.
- Generate the SQL yourself to retrieve the data.

Personally, I would think you'd want to go with the second method so that you can get a better understanding of what's going on in your program.  You have the basic idea.

- Connect to your database
- Query your database to return the data you want.
- Parse the data
- Update your form based on the parsed data.

There's a lot of tutorials out there, but maybe this will get you started?
Avatar of TSTechNA

ASKER

jdavistx,

In another moment i would like to explain me better the option of Crystal Reports, so, i can be able to present reports to my managers.

In this case, the application i'm working on, is for inventory, input, and output,
case input: i want to get from a textbox the partno, so i can search the part in my database, if it exists then ask in another textbox how many articles from this partno i'm gonna add to my stock

As you say, that's what i need to learn, working with data from database.
I can connect to the database, then where this data is, how i can manipulate, then how i update the database after validating and computing.

ASKER CERTIFIED SOLUTION
Avatar of jdavistx
jdavistx

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The first link helped a lot, I made a test, and i inserted data into the database.
Now, getting back,
How i bring data from a table of a database onto my variables in C#?
Well, I suppose a very simple example would be something like:




try
{
	using( var sqlConn = new SqlConnection(connectionString) )
	{
		sqlConn.Open();
		
		string s;
		var cmd = new SqlCommand("SELECT column FROM table", sqlConn);
		s = (string)cmd.ExecuteScalar();
		MessageBox.Show(s);
	}
}
catch(Exception e){ MessageBox.Show("There was an error when trying to retrieve the data!\n\nThe error is as follows:\n" + e.ToString(), "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Error);	}

Open in new window

I'll try that code tonight and i'll let you know

I made a maintenance form, where i can modify the database from a DataGridView, and it was easy, cause i did it all in design mode, just configuring the dataset with a wizard and dragging the table to the form.

But when, i click the save(update) button in the top bar, i get the next error

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

Do i need to modify some part of the code? o how i can solve this? this tool was very helpful to feed inventory data.
Or I need to make a form and make a command with the Update ?

This error i get, when i'll do the same locally in my laptop, everything's good, no error.

At my laptop i'm using SQL Server 2008, sa user, then, with a network with servers, i'm using SQL Server 2005 with sa user also
 
If I remember right, you should use a SqlCommandBuilder to generate those commands before you call Update on your SqlDataAdapter.
Great!, With these tutorials and the fast answer to my questions i finally made what i want.
There's a lot more to learn
thanks