Solved

working con binding combobox

Posted on 2010-08-26
8
319 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
  • 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
 

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
Viewers will learn how the fundamental information of how to create a table.

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now