Okay I've come to an point where I have no idea what to do anymore, I've posted a question before and the result was a -3 in votes to close down the question, and in fear of that happening again kept me searching the googleing till snapped... I've tried numerous code and every single time i get an error...
I've created a database about 6/7 times today and kept changing the connection string...
I've got a database with 3 tables, 1 table is called Company, now what I wanna do is I want to use 2 textboxes representing CompName and password and a dropdownlist representing businesstype.
I want to use these to enter information into the table, can someone please help me
Here's my code:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class CompanyLogin : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConncetionString"].ConnectionString;
string sql = "INSERT INTO company (CompName, BusinessType, Pword) VALUES('" + txtCompName.Text + "','" + DropDownList1.Text + "', '" + txtPassword + "')";
using (SqlConnection sqlconn = new SqlConnection(ConnectionString))
{
sqlconn.Open();
using (SqlCommand command = new SqlCommand(sql, sqlconn))
{
command.ExecuteNonQuery();
}
}
}
}
Error:
System.NullReferenceException: Object reference not set to an instance of an object.
on line:
string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConncetionString"].ConnectionString;
web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="Varsity_College.tempsConnectionString"
connectionString="Data Source=JAUN- PC\SQLEXPRESS;Initial Catalog=Varsity_College.temps;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
...... (irrelevant) ......
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
My error keeps POINTING TO
Line 17:
string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;*
MyConnectionStringin the connectionStrings tag in Web.config. For example<add name="MyConnectionString" connectionString="Data Source=SQLServerNameOrIP;Initial Catalog=DatabaseName;User ID=user;password=****;Network=IDontKnow" providerName="System.Data.SqlClient"/>