473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to register a user to a database?

Hi all,

I'm creating a website using C#/ASP.NET and I keep getting the same error, time and time again and I do not know for the life of me how to fix it. I have been trying, but here's the code;

Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void Page_Load(object sender, EventArgs e)
  3.     { 
  4.         if (IsPostBack)
  5.         {
  6.             SqlConnection connec = new SqlConnection("Data Source=valve; Initial Catalog=m_soft20151_n0266710; Integrated Security=True");
  7.             connec.Open();
  8.             string cmdStr = "SELECT COUNT(*) FROM Users WHERE userName='" + TextBox1.Text + "'";
  9.             SqlCommand findUser = new SqlCommand(cmdStr, connec);
  10.             int temp = Convert.ToInt32(findUser.ExecuteScalar().ToString());
  11.             connec.Close();
  12.  
  13.             if (temp == 1)
  14.             {
  15.                 Response.Write("This username already exists.");
  16.             }
  17.         }
  18.     }
  19.  
  20.  
  21.     protected void Button1_Click(object sender, EventArgs e)
  22.     {
  23.  
  24.         SqlConnection connec = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
  25.         connec.Open();
  26.         string insCmd = "INSERT INTO Users ([userName], [Password], [E-mail]) VALUES (@userName, @Password, @E-mail)";
  27.         SqlCommand insertUser = new SqlCommand(insCmd, connec);
  28.         insertUser.Parameters.AddWithValue("@userName", TextBox1.Text);
  29.         insertUser.Parameters.AddWithValue("@Password", TextBox2.Text);
  30.         insertUser.Parameters.AddWithValue("@E-mail", TextBox4.Text);
  31.  
  32.         try
  33.         {
  34.             insertUser.ExecuteNonQuery();
  35.             connec.Close();
  36.             Response.Redirect("Login.aspx");
  37.         }
  38.         catch (Exception er)
  39.         {
  40.             Response.Write("Please ensure that all fields are completed.");
  41.         }
  42.  
The error occurs with the SQLConnection, "System.NullReferenceException: Object reference not set to an instance of an object.". Does anyone have an idea?

Thanks in advance!
May 25 '11 #1
2 2738
MrMancunian
569 Expert 512MB
On what line does the error occur? Did you check the value of ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString in runtime? If not, give it a breakpoint and add a Watch.

Steven
May 26 '11 #2
Frinavale
9,735 Expert Mod 8TB
Laura Wilkinson,

A NullReferenceException occurs when you try to use something that has not been instantiated yet.

For example, the following will throw an null reference exception
Expand|Select|Wrap|Line Numbers
  1. Person myPerson; 
  2. myPerson.EyeColour = "Blue"; //<-this line throws the exception because myPerson is Null
This exception was thrown because myPerson was not instantiated. In other words, myPerson was Null (or Nothing in VB.NET)

The following is a correction for the error:
Expand|Select|Wrap|Line Numbers
  1. Person myPerson; 
  2. myPerson = new Person(); <-- creates a new instance of Person: so myPerson is not Null
  3. myPerson.EyeColour = "Blue"; 
When you retrieve information from somewhere (like a Database, a Method, a WebService...etc.) you should always check if something was actually returned. If something went wrong retrieving the information from the database then Null/Nothing will be returned to you (or a different exception will be thrown).

You should check for Null before using any object that you have not created yourself.

-Frinny
May 30 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Arijit | last post by:
Hi , I have a file presumably backup of a User database from a SQL Server(2k) database. I want to load/restore that in a local SQL Server to be able to see the table , views , etc present in the...
1
by: B-BoB | last post by:
Hi All, Would someone give me an explanation on how SQL 2000 server backs up a user database? Is SQL server make a copy of user database first, and then backup it up to a defined backup device?...
2
by: RipTide | last post by:
Background: Using an unsupported/abandoned multi-user multi-database program that uses Access 97 and Jet 3.5. Program itself appears to have been built with PowerBuilder 6.5. Databases reside on...
0
by: marko | last post by:
Hi all! I'm trying to make a access database for my fathers shop and I'm pretty stuck so if anyone could help me that would mean a great deal for me! I would like to make a database in wich the...
1
by: ATJaguarX | last post by:
I have an asp.net web application that I'm having problems with. Everytime I view certain pages in design mode, Sourcesafe asks me to check out the page because of requested changes. When I go to...
1
by: socasteel21 via AccessMonster.com | last post by:
I have a multi-user database that stores warranty claims. I would like to be able to export all critical tables to a backup database when the introductory form is opened. The problem is the...
3
by: jonosborne | last post by:
I know this is very long-winded but I am trying to include all information. I currently manage a data input database which is used within a multiple user environment. This data is stored within...
1
by: lenygold via DBMonster.com | last post by:
Is there is a way in DB2 to update user id Special register USER ? Thank's in advance. -- Message posted via DBMonster.com http://www.dbmonster.com/Uwe/Forums.aspx/ibm-db2/200807/1
1
by: thaixyz | last post by:
Hi, I am migrating SQL 2000 database from one machine to another using the detach/attach method. I used the TSQL script sp_help_revlogin procedure provided by microsoft to copy the logins, this...
2
by: Jimmy | last post by:
Do I need to install additional module in XOOPS to support webservice? Either it's part of the core or I have to download some where to install, if such thing already existed, how can I get the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.