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;
public partial class course : System.Web.UI.Page
{ // string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
// protected void Page_Load(object sender, EventArgs e)
// {
// con.Open();
// }
// protected void Button1_Click(object sender, EventArgs e)
// {
// //SqlConnection con = new SqlConnection(connStr);
// SqlCommand cmd = new SqlCommand("insert into profile values('" + name.Text+ "','" + fname.Text+ "','" + ep.Text+ "','" + cls.Text+ "','" + semester.Text+ "')", con);
////con.Open();
// cmd.ExecuteNonQuery();
////cm.ExecuteNonQuery();
//// con.Close();
SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into profile(studentname,fathername,ep1) values('" +TextBox1.Text + "','" +TextBox2.Text+ "', '" +TextBox3.Text+ "')", con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Registered Successfully";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3. Text = "";
//string s1 = string.Empty;
//foreach (ListItem item in this.checkbox.Items)
//{
// if (item.Selected)
// {
// s1 = item.ToString();
// SqlCommand com = new SqlCommand("Insert into profile(courseId) values('" + s1 + "')", con);
// // con.Open();
// com.ExecuteNonQuery();
// con.Close();
// }
//} Response.Write("Inserted Successfully");
}

Sandeep Singh ShekhawatRank #147 · 15.6K reputationPosted May 21, 2013, 6:12 PM EDT
Your following uncommented code is Okay
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("insert into profile(studentname,fathername,ep1) values('" + TextBox1.Text + "','" + TextBox2.Text + "', '" + TextBox3.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
Label1.Visible = true;
Label1.Text = "Registered Successfully";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
So please check your connection string in web.config that connect to right database.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=SANDEEPSS-PC;database=Tutorial; user=sa;password=******"/>
connectionStrings>
Otherwise you are getting some error then post error on the fourm so we can identify actual reason. One more thing that your profile tables fields are varchar/nvarchar type which are we passing.
Jignesh TrivediRank #16 · 50.3K reputationPosted May 21, 2013, 7:20 PM EDT
hi,
one suggestion here.
define scope of each variable. for example do not create connection and open it on page load instead of doing this create and open connection whenever it required.
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("insert into profile values('" + name.Text+ "','" + fname.Text+ "','" + ep.Text+ "','" + cls.Text+ "','" + semester.Text+ "')", con);
con.Open();
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
hope this will help you.
Riddhi ValechaRank #794 · 2.9K reputationPosted May 21, 2013, 7:28 PM EDT
Can you share your project ?? It's easier to make changes in the project....
Shankar MRank #874 · 2.5K reputationPosted May 21, 2013, 8:26 PM EDT
Pankaj PandeyRank #642 · 3.7K reputationPosted May 21, 2013, 9:00 PM EDT
con.Open();
mark as answred if helpfull
Sunny SharmaRank #223 · 11.1K reputationPosted May 21, 2013, 9:22 PM EDT
romasha ali98 reputationPosted May 23, 2013, 1:50 PM EDT
using System;
Riddhi ValechaRank #794 · 2.9K reputationPosted May 23, 2013, 9:02 PM EDT
Can you share your project ?? It will be easier to do changes there..
romasha ali98 reputationPosted May 24, 2013, 12:18 PM EDT
Sunny SharmaRank #223 · 11.1K reputationPosted May 24, 2013, 9:41 PM EDT
I went through your code... It's working absolutely fine.
As I started running your application it gave me few SQL Server connection errors because of the configuration settings did not match with mine.
Also it was required to make some changes in IIS configuration. I had to change Process Model Identity to allow application to read the database. I did this by going to my "Application Pools" list > selected "ASP.NET v4.0" >right click > Advanced Properties > under "Process Model" tab > Identity > Choose NetworkService from the dropdown list > ok >ok.
After rectifying all them I got this error finally:
"Failed to update database "C:\INETPUB\WWWROOT\APP_DATA\DATABASE.MDF" because the database is read-only.."
I solved this by grating write access to IIS_USERS on App_Data folder.
Finally I reset the IIS Server using iisreset.exe on command line with Admin rights and it all worked like a charm (You can see the snap for data that i added in).
Hope this helps!
Thanks.