?
Solved

working con binding combobox

Posted on 2010-08-26
8
Medium Priority
?
329 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

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