Solved

working con binding combobox

Posted on 2010-08-26
8
326 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:TSTechNA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:jdavistx
ID: 33533265
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?
0
 

Author Comment

by:TSTechNA
ID: 33533722
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.

0
 
LVL 7

Accepted Solution

by:
jdavistx earned 500 total points
ID: 33533835
Well, you can't use Crystal Reports to insert data in to your database.  You could embed a Crystal Report in to your C# app, but let's not worry about that.

There's no shortage of tutorials for C# database applications:
http://www.csharphelp.com/2007/04/designing-a-winform-in-c-and-linking-it-to-a-sql-server-database/
http://www.dreamincode.net/forums/topic/31314-sql-basics-in-c%23/
http://www.codeproject.com/KB/database/sql_in_csharp.aspx

I think there's even some pretty detailed ones available on MSDN, but I can't seem to find them at the moment.

Any of the above, I would think, would be able to get you started with a simple C#/SQL app.
0
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 

Author Comment

by:TSTechNA
ID: 33546934
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#?
0
 
LVL 7

Expert Comment

by:jdavistx
ID: 33557971
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

0
 

Author Comment

by:TSTechNA
ID: 33563390
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
 
0
 
LVL 7

Expert Comment

by:jdavistx
ID: 33581795
If I remember right, you should use a SqlCommandBuilder to generate those commands before you call Update on your SqlDataAdapter.
0
 

Author Closing Comment

by:TSTechNA
ID: 33611844
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
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question