Hi all
I was designing an registration system in which i want that the info (Name, user name, pass, country) should go to the table in SQL server express.
Database Info : i have created an ShareSignup database in which there is one table named tblReg having columns Name, Username, pass, country.
The database is in local computer (my computer) in SQL Server 2008 Express and i use MyPC\SQLEXPRESS as server name to login to the SQL Server.
Now i was designing a registration system in which i have a SignUp.aspx page, i added following code to web.config page (for connecting to Database)
<connectionstrings>
<add name="MyCon" connectionstring="server=(local);Initial Catalog=ShareSignup;Integrated Security=SSPI;" providername="System.Data.SqlClient" />
</connectionstrings>
Now on the next for SignUp.aspx.cs page i added the code
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
}
private void ExecuteInsert(string name, string uname, string pass, string country)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO tblReg (Name, UserName, Password,Country) VALUES "
+ " (@Name,@UserName,@Password,@Country)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[4];
param[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);
param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
param[2] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
param[3] = new SqlParameter("@Country", SqlDbType.VarChar, 15);
param[0].Value = name;
param[1].Value = uname;
param[2].Value = pass;
param[3].Value = country;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TxtPassword.Text == TxtRePassword.Text)
{
ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, TxtAddress.Text);
Response.Write("Record was successfully added!");
}
else
{
Response.Write("Password did not match");
TxtPassword.Focus();
}
}
Now on runing the page and hitting Submit (button1) i get this error.
Object reference not set to an instance of an object.
and it shows the error at line
System.Configuration.ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;
what can be the problem ?
may be in web.config code ?
or may be with setting <connectingString> ?
Please help me out
Thanks