Working with Sql database

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 5253 Views - Last Post: 14 February 2010 - 07:42 AM Rate Topic: -----

#1 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Working with Sql database

Posted 12 February 2010 - 04:05 AM

Hello! Here a project on which I am working on currenty...what it does is it asks for the login information like username and password from user and verifies it with the values in the database..and if it matches with the values in the database it opens 2nd window where It gives user to change password.....
What I have done till now is stared a database named DB.mdf which has a table named userInfo_table and connected with the program...I am able to validate the user name and password from the user and open the second window but now I cant overwrite the value of password in the database so that from next time when user Logins user have to insert the new password...
Database details...
database name -- DB.mdf
Table name -- usrInfo_table
Columns -- Username and Password

please evaluate my program

This post has been edited by rocky_upadhaya: 12 February 2010 - 08:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Working with Sql database

#2 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Working with Sql database

Posted 12 February 2010 - 05:51 AM

You should specify that you are using WPF. There is a difference between WinForms and WPF. You should also post in the WPF sub-forum located here.(hint, it's also at the top of the C# forum)

You should also specify that you are using LINQ-To-SQL or Entity Framework(I can't tell which you are using since they both use the ".dbml" file).
Was This Post Helpful? 0
  • +
  • -

#3 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: Working with Sql database

Posted 12 February 2010 - 05:58 AM

I Intentionally didn't post this in wpf forum because my problem is of database not regarding winforms and wpf...I has nothing to do with GUI end...or should i have to shift it...if yes how to shift it over there??

and oh I have used LINQ-To-SQL in this project...Any Help would be very much appreciated because I am stuck in this problem since yesterday....Thanks

This post has been edited by rocky_upadhaya: 12 February 2010 - 05:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Working with Sql database

Posted 12 February 2010 - 06:01 AM

here would be the proper way to do the login...

private void button2_Click(object sender, RoutedEventArgs e)
{
    // creates an instance of the data context class
    using (var db = new DataClasses1DataContext())
    {
        // gets an object of the "usrInfo_table" type where the Username is equal to the Text in the username textbox
        //     and where the Password is equal to the password in the password textbox.
        // You should use SingleOrDefault rather than Single, because Single will throw an exception if no record is found.
        //    SingleOrDefault will return null if no record is found.
        var user = db.usrInfo_tables.SingleOrDefault(c => c.Username == frm1_usrName.Text.Trim() && c.Password == frm1_Pass.Text.Trim());

        // user is null if no record was found with the specified username and password
        if (user == null)
        {
            MessageBox.Show("Incorrect Username or Password!!");
            return;
            // "return" will stop the execution of the method
            //   none of the code below will run
        }

        win2.Show();
        this.Close();
    }
}


Was This Post Helpful? 1
  • +
  • -

#5 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Working with Sql database

Posted 12 February 2010 - 06:07 AM

now for the changing of the password, you could do this...

private void button1_Click(object sender, RoutedEventArgs e)
{
    using (var db = new DataClasses1DataContext())
    {
        // this should look familiar
        var user = db.usrInfo_tables.SingleOrDefault(c => c.Username == txt_usrname.Text.Trim() && c.Password == txt_oldpass.Text.Trim());

        if (user == null)
        {
            MessageBox.Show("Incorrect Username or Old Password!!");
            return;
        }

        user.Password = txt_newpass.Text.Trim();
        db.SubmitChanges();
    }
}



EDITED: hmmm... After testing this, it doesn't actually update the password. The code is correct. Not sure why it doesn't update.

Also, why are you attaching a SQL Express database to the project? Is this going to be a database that is local to the PC? If so, you should use a SQL Compact database instead. That's what a SQL Compact database was designed for.

This post has been edited by eclipsed4utoo: 12 February 2010 - 06:54 AM

Was This Post Helpful? 1
  • +
  • -

#6 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: Working with Sql database

Posted 12 February 2010 - 07:12 AM

Oh! thanks for the suggestion! I wiil change it to SQL Compact database and I have also tried the similar thing for changing the password...now that since you have also suggested me the same thing I am totally why is the data base not updating.....Please let me know if this work...Thanks
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Working with Sql database

Posted 12 February 2010 - 07:23 AM

View Postrocky_upadhaya, on 12 February 2010 - 09:12 AM, said:

Oh! thanks for the suggestion! I wiil change it to SQL Compact database and I have also tried the similar thing for changing the password...now that since you have also suggested me the same thing I am totally why is the data base not updating.....Please let me know if this work...Thanks


remember that SQL Compact isn't "officially supported" in Visual Studio 2008 with LINQ-to-SQL, but .Net does support it(weird huh?). You can use this to make it work.

http://pietschsoft.c...ct-Edition.aspx

It's really the LINQ-To-SQL Designer that has the problems. I've used this and it works fine.

This post has been edited by eclipsed4utoo: 12 February 2010 - 07:24 AM

Was This Post Helpful? 1
  • +
  • -

#8 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: Working with Sql database

Posted 12 February 2010 - 07:35 AM

Oh! is this the only way to do this? because my level of currently not able to understand it properly....what is the dis advantage of using SQL Express database here?? is there any alternative to this or would it be better to start looking the link given by you??....
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Working with Sql database

Posted 12 February 2010 - 07:42 AM

View Postrocky_upadhaya, on 12 February 2010 - 09:35 AM, said:

Oh! is this the only way to do this? because my level of currently not able to understand it properly....what is the dis advantage of using SQL Express database here?? is there any alternative to this or would it be better to start looking the link given by you??....


The link I sent you will walk you through adding a SQL Compact database to your project and then creating the .dbml file for it. It's really simple. The only reason you have to do this is because the LINQ-To-SQL designer in VS 2008 doesn't support SQL Compact for the creation of the .dbml file. Once you create the .dbml file, you can add it to the project and then open it just as if VS created it and it will work the same.

Not sure why VS 2008 doesn't support the creation of a .dbml file for a SQL Compact database, but allows you to open a .dbml file that was already been created.
Was This Post Helpful? 1
  • +
  • -

#10 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: Working with Sql database

Posted 12 February 2010 - 08:25 AM

As you have said You have tried the same thing in Compact SqlDatabase Please upload it in here or email it to me at [email protected]
I tried the procedures of the links but is at stuck at no 4. It says
Error: SQM1003: Input file 'DB.sdf' does not exist....
Was This Post Helpful? 0
  • +
  • -

#11 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: Working with Sql database

Posted 12 February 2010 - 08:54 AM

I have successfully added .dbml file in the project and it even shows up in the solution explorer...now what should I do next?? I will be looking forward for your help...thanks...
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Working with Sql database

Posted 12 February 2010 - 09:17 AM

View Postrocky_upadhaya, on 12 February 2010 - 10:25 AM, said:

As you have said You have tried the same thing in Compact SqlDatabase Please upload it in here or email it to me at [email protected]
I tried the procedures of the links but is at stuck at no 4. It says
Error: SQM1003: Input file 'DB.sdf' does not exist....


in the Visual Studio Command Prompt, you need to navigate to the folder where the .sdf file is. It's telling you that it can't find the .sdf file in the current directory.

View Postrocky_upadhaya, on 12 February 2010 - 10:54 AM, said:

I have successfully added .dbml file in the project and it even shows up in the solution explorer...now what should I do next?? I will be looking forward for your help...thanks...


You can use the Server Explorer to create a connection to the .sdf file so you can add tables to the database. Once those tables are added, drag them onto the designer form as normal. Then you should be good to go.

EDITED: Told you wrong on that last part. You will need to create the tables in the database, then do the SqlMetal generation. You won't be able to do the drag-drop in the L2S Designer.

This post has been edited by eclipsed4utoo: 12 February 2010 - 09:28 AM

Was This Post Helpful? 1
  • +
  • -

#13 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: Working with Sql database

Posted 12 February 2010 - 09:32 AM

OH! yes I was about to say that...The Zip file I have now uploaded is updated one please have a look through it what else is missing? because it says it is missing namespace assembly....
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1533
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Working with Sql database

Posted 12 February 2010 - 09:44 AM

View Postrocky_upadhaya, on 12 February 2010 - 11:32 AM, said:

OH! yes I was about to say that...The Zip file I have now uploaded is updated one please have a look through it what else is missing? because it says it is missing namespace assembly....


you are including a namespace that isn't a namespace. Also, you are trying to reference your datacontext as "DBDataContext". The name of your data context is "DB".

Open "MyDatabase.designer.cs" file, and the name of the class is the name of your DataContext object. If you want to change that, change the "DB" to "DBDataContext", then replace all using the red "underline" that comes up.
Was This Post Helpful? 1
  • +
  • -

#15 rocky_upadhaya   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 09-January 10

Re: Working with Sql database

Posted 12 February 2010 - 10:03 AM

did you mean
 using (var db = new DBDataContext()) //in the windows1.xaml.cs


public partial class DBDataContext : System.Data.Linq.DataContext//in the MyDatabase.designer.cs
	{



It comes with compile time error that says....
Error	1	'First_DataBase.DBDataContext' does not contain a constructor that takes '0' arguments	


This post has been edited by rocky_upadhaya: 12 February 2010 - 10:05 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2