using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class FormCreationWithDataStoraget : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void sbmt_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["Data Source=RAJIM -PC;Initial Catalog=RajiDatabase;Integrated Security=True"].ConnectionString;
string insertSql = "INSERT INTO FamilyDetails(FirstName,LastName,Gender,Age,Relationship,MobileNumber)" + "values(@FirstName,@LaseName,@Gender,,@Age,@,@MobileNumber)";
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand scmd = new SqlCommand();
scmd.Connection = conn;
scmd.CommandType = CommandType.Text;
scmd.CommandText = insertSql;
SqlParameter firstname = new SqlParameter("@FirstName", SqlDbType.VarChar, 40);
firstname.Value = tbx.Text.ToString();
scmd.Parameters.Add(firstname);
SqlParameter lastname = new SqlParameter("@LaseName", SqlDbType.VarChar, 40);
lastname.Value = tbx1.Text.ToString();
scmd.Parameters.Add(lastname);
SqlParameter gender=new SqlParameter("@Gender",SqlDbType.VarChar,40);
gender.Value = rbt.SelectedItem.ToString();
scmd.Parameters.Add(gender);
SqlParameter age = new SqlParameter("@Age", SqlDbType.Int);
age.Value = tbx2.Text.ToString();
scmd.Parameters.Add(age);
SqlParameter relationship = new SqlParameter("@Relationship", SqlDbType.VarChar, 40);
relationship.Value = tbx3.Text.ToString();
scmd.Parameters.Add(relationship);
SqlParameter mobilenumber=new SqlParameter("@MobileNumber",SqlDbType.VarChar, 10);
mobilenumber.Value = tbx4.Text.ToString();
scmd.Parameters.Add(mobilenumber);
try
{
conn.Open();
scmd.ExecuteNonQuery();
Response.Write("User Registration successful");
}
catch (SqlException ex)
{
string errorMessage = "Error in registering user";
errorMessage += ex.Message;
throw new Exception(errorMessage);
}
finally
{
conn.Close();
}
}
}
Asked
Active
Viewed 374 times
-2

LittleBobbyTables - Au Revoir
- 32,008
- 25
- 109
- 114

rajendran m
- 1
- 3
-
Have you debug your code? Do you know what line exactly is breaking it? Possible duplicate: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – SOfanatic Oct 09 '14 at 13:09
-
string connectionString = ConfigurationManager.ConnectionStrings["Data Source=RAJIM -PC;Initial Catalog=RajiDatabase;Integrated Security=True"].ConnectionString; ......This is the Error line – rajendran m Oct 09 '14 at 13:18
-
post your web.config where your connection strings are being defined. – SOfanatic Oct 09 '14 at 13:29
-
I didn't Add any code on Web.Config File – rajendran m Oct 09 '14 at 13:48
-
This is my We.config file:
-
Next time you ask a question please provide a little more information rather than just pasting in a chunk of code. – bwegs Oct 19 '14 at 03:14
2 Answers
1
It looks like this line is causing your problem:
string insertSql = "INSERT INTO FamilyDetails(FirstName,LastName,Gender,Age,Relationship,MobileNumber)" + "values(@FirstName,@LaseName,@Gender,,@Age,@,@MobileNumber)";
Specifically:
@FirstName,@LaseName,@Gender,,@Age,@,@MobileNumber
It Should Be:
@FirstName,@LaseName,@Gender,@Age,@Relationship,@MobileNumber

How 'bout a Fresca
- 2,267
- 1
- 15
- 26
-
Still I'm getting the same Error..the error line is (string connectionString = ConfigurationManager.ConnectionStrings["Data Source=RAJIM -PC;Initial Catalog=RajiDatabase;Integrated Security=True"].ConnectionString;) – rajendran m Oct 09 '14 at 13:17
-
Runtime Error (Object reference not set to an instance of an object.) – rajendran m Oct 09 '14 at 13:27
0
The issue is that this line:
string connectionString = ConfigurationManager.ConnectionStrings["Data Source=RAJIM -PC;Initial Catalog=RajiDatabase;Integrated Security=True"].ConnectionString;
is not retrieving any object because you don't have a ConnectionString
defined your web.config
with the name of "Data Source=RAJIM ..." so basically calling the .ConnectionString
at the end of it breaks it.
Add the "Data Source=RAJIM..." line as a connection string in your web.config
(more info on connection strings here).
Once you give your connection string a name, i.e., "MyConnectionString" then change your code to be:
string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

SOfanatic
- 5,523
- 5
- 36
- 57
-
Added the data source in Web.config file but now its showing runtime error on "throw new Exception(errorMessage);" line..Error name is (Error in registering userIncorrect syntax near ','.) – rajendran m Oct 09 '14 at 14:33
-