Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 14th, 2006, 04:02 PM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default help required in C# and SQL server

hi everyone, can anyone please help me generating code for checking the username availabilty in my database(sql server) before inserting it & display message "user exists" if the username already exists.

thank u

its very urgent.
 
Old December 14th, 2006, 05:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi anuMScIT,

If it's so urgent, you better spend some time explaining the problem in more detail. Tell us what you have done so far, what you tried, what code you have looked at, what works and what doesn't, what environment you're in (Windows Forms, Web Forms etc) and so on and so on.

Also, a better subject than Hiiiii really helps in getting people's attention.

So: if it's really that urgent, do some proper preparation and come back to the forum with a better formulated question. Take a look here for some inspiration: http://www.catb.org/~esr/faqs/smart-questions.html

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old December 15th, 2006, 12:56 AM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i have used select query n executed as executnonquery() then i have checked n done as following

string check="select * from UserMaster where UserName like '" + txtuserid.Text + "'";

             SqlCommand cm = new SqlCommand(check, conn);
             int res = cm.ExecuteNonQuery();
            if(res>=1)

            {
                lbldisplay.Text = "name already exists";
            }

            else
            {
                SQL = "insert into usermaster (UserName,Password1,FirstName,LastName,UserLevel,E mailId,DateCreated,Status) values ('" + txtuserid.Text + "','" + txtpassword.Text + "','" + txtfirstname.Text + "','" + txtlastname.Text + "','" + drpgrp.SelectedIndex + "','" + txtemail.Text + "','" + System.DateTime.Now + " ' ,1)";
                SqlCommand cmd = new SqlCommand(SQL, conn);


                cmd.ExecuteNonQuery();
            }

 
Old December 15th, 2006, 03:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Instead of ExecuteNonQuery, you should use ExecuteScalar in combination with SELECT COUNT in your SQL statement.

Your current code (ExecuteNonQuery) is used to perform action queries like INSERT, UPDATE and DELETE, and it cannot be used to select records or retrieve a count.

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

 
Old December 15th, 2006, 07:46 AM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank u Imar for ur support............finally i cud solve it.

 
Old December 15th, 2006, 09:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome.

For the benefit of the archive of this forum, would you mind sharing your final solution?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 20th, 2006, 07:26 AM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can anyone please help me in creating dynamic menu taking data from my sql database. ihave a main menu table and sub menu table and the contents in it will be as follows:
MainMenuID MainMenuName

1 Year 1
5 Year 2
11 Year 3
15 Year 4
21 Year 5
25 Seminar & Projects
97 User Actions
98 User Actions
99 Reports

MainMenuID MenuID MenuName FileName

1 1 Fund. Of Computers DisplayFiles.aspx
1 2 Q Basic DisplayFiles.aspx
1 3 Office DisplayFiles.aspx
1 4 C DisplayFiles.aspx
1 5 Electronics DisplayFiles.aspx
1 6 Maths - I DisplayFiles.aspx
1 7 Maths - II DisplayFiles.aspx
1 8 Practical I DisplayFiles.aspx
1 9 Practical II DisplayFiles.aspx
1 10 DS DisplayFiles.aspx

5 21 subject name DisplayFiles.aspx
5 22 subject name DisplayFiles.aspx
5 23 subject name DisplayFiles.aspx
5 24 subject name DisplayFiles.aspx
5 25 subject name DisplayFiles.aspx
5 26 subject name DisplayFiles.aspx
5 27 subject name DisplayFiles.aspx
5 28 subject name DisplayFiles.aspx
5 29 subject name DisplayFiles.aspx
5 30 subject name DisplayFiles.aspx

and so on....
i will be realy thankful to the one who helps me in doing it.


 
Old December 20th, 2006, 07:33 AM
Registered User
 
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the solution to above problem with executenonquery is as below

 SqlConnection conn = new SqlConnection(source);
            conn.Open();
            string SQL;
            string check = "select UserName from UserMaster where UserName like '" + txtuserid.Text + "'";


SqlCommand cm = new SqlCommand(check, conn);

            SqlDataAdapter da = new SqlDataAdapter(check, conn);
            DataSet ds = new DataSet();
            int res = da.Fill(ds, "UserMaster");

            if (res >= 1)
            {
                lbldisplay.Text = "name already exists";






Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL XML Explicit (Output file required) Neal SQL Server 2005 0 August 4th, 2008 03:04 AM
How to write T-SQL to retrieve the required Output anilkumar.dwivedi SQL Language 3 June 26th, 2008 12:37 AM
SQL help required Teessider_2000 MySQL 1 January 10th, 2007 02:15 AM
Help required with VB SQL Server 7.0 Book.. danfeather SQL Server 2000 1 September 29th, 2005 08:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.