-
Questions regarding C#
Hello!
I have several questions regarding C# language. I am a begginer programmer and I am working on a project. here are my questions:
1. I am using VS C# 2008. I want to use a local DB with my program (a file that will sotre information). I am thinking about using the SQL CE server, uncluded with the VS. I use local database user control from visual studio and create a DB file, but how can I connect to it (a sample script would be nice).
2. Using panels - how can I make multiple panels on one form - on button click I change the things in the form (ets say textbox), without changing the form.
3. How can I make an dynamic menu/app from the DB - Lets say I have 2 columns in the DB - Name and Information - WHen i get in a form (Form1), I get an dynamic menu with all the things under Names in the DB and when I click on a name it will open new panel/form, displaying the information for the name (Column Information).
4. Drawing things in C#. Any tutorials, samples, programs are welcome! More specially simetry drawing - point symetry, lines, figures.
I will be really glad If u can answer any of the things. Will really appreciate sample scripts, applications and especially tutorials.
Alex Barakov
-
Re: Questions regarding C#
For no 4. Here are some Links
http://www.deitel.com/Books/VisualC2...6/Default.aspx
here download the Code examples into Your computer. There Are tons of useful example Programs Project including drawing and shapes. You just go through all the examples, many of this will surely help you....
http://msdn.microsoft.com/en-us/magazine/cc721604.aspx
http://stackoverflow.com/questions/1...rawing-program
http://social.msdn.microsoft.com/For...7-8fff11b4139c
-
Re: Questions regarding C#
Thanks, will look at this links right away!
Looking forward to getting more links/tuts/samples on all of the questions above.
-
Re: Questions regarding C#
To create the local DB:
In the menu bar click View->Server Explorer.
In the Server Explorer right click Data Connections and click add connection
Select Microsoft SQL Server Database File
Give the DB a name, a save it to you harddrive.
To create tables in the database file:
click the plus on the newly created database connection
right click tables and select Add New Table
input the table structure
To add the database to the project:
In the Solution Explorer right click your project and select Add->New Item
Select LINQ to SQL Classes, name it and click Add
Double click the newly created item in the Solution Explorer
Now drag the table from the Server Explorer into the middle of VS (the white space that is now there)
You will be asked if you would like to add the Database Connection to the project, select Yes.
Now you sould see a model of you table.
Save the .dbml file
To interact with the database:
create a new database object:
Code:
var db = new dbDataContext(); // if your .dbml file is called db
var db = new databaseDataContext(); // if your .dbml file is called database
Now you can access the data in the database using LINQ to SQL
e.g.:
Code:
var row = db.SomeTable.Where(a => a.id == 4); // SELECT * FROM SomeTable WHERE 'id' = '4';
-
Re: Questions regarding C#
Thank you for the answers. Still looking forward to getting more things!
About that database. Can you give me an example about:
A databse nammed testdb
A table named users
with columns: ID and User
Lets say I have winforms application with 2 text boxes and 2 buttons. When I enter the User text in textBox1 and press button1, it will import the data from it into the DB (under ID 1, if and using autoincrease). And when I click button 2, it will show the data where the ID is 1 to textBox2.
Thanks in advance!
-
Re: Questions regarding C#
Quote:
A databse nammed testdb
A table named users
with columns: ID and User
Lets say I have winforms application with 2 text boxes and 2 buttons. When I enter the User text in textBox1 and press button1, it will import the data from it into the DB (under ID 1, if and using autoincrease). And when I click button 2, it will show the data where the ID is 1 to textBox2.
something like this:
Code:
private void button1_Click(object sender, EventArgs e)
{
var db = new testdbDataContext();
db.Users.InsertOnSubmit(new User() { User1 = textBox1.Text }); //ID is auto assigned and incremented
db.SubmitChanges();
}
private void button2_Click(object sender, EventArgs e)
{
var db = new testdbDataContext();
var user = db.Users.Single(a => a.ID == 1); // get the user with ID = 1
textBox2.Text = user.User1;
}
The autogenerated names of the columns in the table might differ from the names that you have given the columns in the database.
In this case it is because the table and the column shares the same name.
It would make more sense to call the column 'username' or something like that.
-
Re: Questions regarding C#
I am working with databse: databse
table: Users
Columns: ID (primary key, auto increase), Username and Password
I am using the following code:
var db = new databaseDataContext();
db.Users.InsertOnSubmit(new User() { Username = textBox1.Text }); //ID is auto assigned and incremented
db.SubmitChanges();
It is not inserting any values in the DB, but I can't understand waht is this doing:
db.Users.InsertOnSubmit(new User() { Username = textBox1.Text });
especially the thing "new User()", as I don't have anything ithe DB called 'User' and IF i change it to anything else i get errors... I will be glad If you can tell me why it is not working and what does that row mean.
Thanks for the answers.
PS. Maybe we can talk over on msn/skype, in case I have mroe answers and you don't mind answering them..
-
Re: Questions regarding C#
I am working on similar project too, so was also a great help for me too....but now my problem is little diffrent.....
in table users I have 2 columns called username and password...I have already Inserted a value for each columns....I dont have any Intention to adding further data to it for now....I now want to store these values of Username and password from the database and store it in the string variavles (say s1 and s2)......Please Help me here
-
Re: Questions regarding C#
I have successfully got that value and stored it in string....like in
Code:
var usrnm = db.usrInfo_tables.FirstOrDefault();
string uname = usrnm.Username.ToString();
MessageBox.Show(uname);
But after this I changed the value of username from table from server explorer but the message box showed the earlier value....what should I do to change the value in database and in the program too??
-
Re: Questions regarding C#
I have noticed an delay on this. The program just doesen't seem to show the right that right away, you need to turn it off, refress the DB, wait a couple of seconds and than turn it on again. Don't know why's that and I honestly don't care.
Sorry for beeing rude, but this is my topyc and I don't like it beeing spammed!
-
Re: Questions regarding C#
What?? you mean you dont like me getting involved in your thread??
-
Re: Questions regarding C#
Um, why don't you make ur own thread for asking questions??
-
Re: Questions regarding C#
You are going all wrong here! Since I was working on almost same project here i thought it would be better if I would also participate in this discussion coz it would help both of us...BTW this is an public discussion forum where we all share the knowledge we have with others....Some people here are highly qualified professionals who spend their valuable time helping the one who want to learn like you and me.....Remember they are not your personal tutors.....I have not messed up your problems or taken your topic to another path....I just continued your thread coz I thought our problem was similar............anyway I will not continue this thread from now....Take care! and stop treating this forum as your personal Homepage
-
Re: Questions regarding C#
Quote:
Originally Posted by
rocky_upadhaya
You are going all wrong here! Since I was working on almost same project here i thought it would be better if I would also participate in this discussion coz it would help both of us...BTW this is an public discussion forum where we all share the knowledge we have with others....Some people here are highly qualified professionals who spend their valuable time helping the one who want to learn like you and me.....Remember they are not your personal tutors.....I have not messed up your problems or taken your topic to another path....I just continued your thread coz I thought our problem was similar............anyway I will not continue this thread from now....Take care! and stop treating this forum as your personal Homepage
I am sorry about this, but I don't think our problems were similar. Anyway you are right about the forums - this is a community, but the things you have written help me in no way.
Thanks anyway if you thought you can help.
-
Re: Questions regarding C#
No! I have implemented all the suggestion of jonlist and my project is similar to your's have a look at my thread "starting Datatbase" http://www.codeguru.com/forum/showthread.php?t=492538 the difference is how we implement it....wish I could help you but like new I am also new to this language and this is my first database application download and evaluate mine project....
-
Re: Questions regarding C#
OK in that case, sorry about my missunderstanding.
-
Re: Questions regarding C#
A solution to one of my problems: Inserting data in DB:
databaseDataContext db = new databaseDataContext();
var newCustomer = new User
{
Username = textBox1.Text,
Password = textBox2.Text,
};
db.Users.InsertOnSubmit(newCustomer);
db.SubmitChanges();
Hope this helps to somebody. Now I want to create something like a menu based on a DB:
A table named Table1
with columns: ID(prim key, auto increase)
Name
Information
A winform that will display the data from Column Name like links and when I click an record on that menu it will open form2 and will display the things from Information in textbox. I am sure that ti can be done easily, but can't actually do it myself.
-
Re: Questions regarding C#
Dude what is the problem with your code or program? please make it clear because someone taught me a way to my problem too and it does not give any error either but my database is not updating at all....
-
Re: Questions regarding C#
We have turned it in to a chat but nevermind... Actually i noticed that this does NOT update the DB.. Not sure what is the problem too. I have a datagridview in my program, 2 textboxes and 2 buttons, when I enter Data1 and Data2 in textbox1 and textbox2 and click button 1 it SHOULD insert te data in the DB, and when I click button 2 the data really shows in the datagrid(!), but when I check the DB there is nothing in it...Its weird, as I am getting the things from a book, but I guess I am wrong somewhere. I will be really glad if someone can solve our problems
-
Re: Questions regarding C#
Usually what happens is the db is copied into the \debug folder when building the project. You run your program which uses the db in the \debug folder, make the changes which get stored in the db and then quit the program.
The next time you run the program, the db from your project gets copied over to the \debug folder so when you open the app it looks like the db didn't get written.
What you can do is temporarily hardcode the path to the db to a fixed location that won't get overwritten (like point it to the db in the project folder, not the \debug folder).
-
Re: Questions regarding C#
Great!! I cant Explain the degree of Happiness right now...Thank You very Much......Leaving the development phase apart does that mean that I can now create the Installer Of my application with a database file in it and it will work on other application??
-
Re: Questions regarding C#
Quote:
Originally Posted by
rocky_upadhaya
Great!! I cant Explain the degree of Happiness right now...Thank You very Much......Leaving the development phase apart does that mean that I can now create the Installer Of my application with a database file in it and it will work on other application??
Who knows? That's what testing is for.
-
Re: Questions regarding C#
Thanks for the asnwers. I have noticed hat myself too yesterday.
Now I am stuck at cerating a database driven menu - a menu that uses that from the database. I can only fins tuts about asp.net and I am working on windows applications...
-
Re: Questions regarding C#
Essentially, you read the data out of a the database and use it to create the menu structure. The actual menu creation code might be a bit different from the ASP.Net, but the rest is the same.
I would start by creating some menus in code. To help with this I would create a single 'universal' menu handler and assign it to all the menu items I've created. If you set the id of the menu item properly, then you should be able to determine what menu item you are acting on in the 'universal' menu handler and then take appropriate action.
Once you can create menu items on-the-fly in code, then you can figure out how to read the menu structure out of the database (and then reuse the code from above to dynamically create the menu items).
Say, if you have any more related questions on menus, please create a new post.
-
Re: Questions regarding C#
I'm coming in at the end here, but if the local database is very simple, consider using an XML file instead of relying on the presence of SQL Server Express etc.
Dataset.Read/WriteXML(fileName).... check it out.
-
Re: Questions regarding C#
well how about using sql compact database. why to use compact instead of sql server edition...
-
Re: Questions regarding C#
Quote:
Originally Posted by
rocky_upadhaya
well how about using sql compact database. why to use compact instead of sql server edition...
Why not type "Sql Server feature comparison" in google and let Microsoft explain the differences to you?
http://msdn.microsoft.com/en-us/library/cc645993.aspx
http://msdn.microsoft.com/en-us/library/ms144275.aspx