 |
| 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
|
|
|
|

December 14th, 2006, 04:02 PM
|
|
Registered User
|
|
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

December 14th, 2006, 05:13 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

December 15th, 2006, 12:56 AM
|
|
Registered User
|
|
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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();
}
|
|

December 15th, 2006, 03:10 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

December 15th, 2006, 07:46 AM
|
|
Registered User
|
|
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thank u Imar for ur support............finally i cud solve it.
|
|

December 15th, 2006, 09:18 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

December 20th, 2006, 07:26 AM
|
|
Registered User
|
|
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

December 20th, 2006, 07:33 AM
|
|
Registered User
|
|
Join Date: Dec 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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";
|
|
 |