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");
}
Sunny SharmaPosted May 25, 2013, 1:41 AM
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.
romasha aliPosted May 24, 2013, 4:18 PM
Riddhi ValechaPosted May 24, 2013, 1:02 AM
Can you share your project ?? It will be easier to do changes there..
romasha aliPosted May 23, 2013, 5:50 PM
using System;
Sunny SharmaPosted May 22, 2013, 1:22 AM
Pankaj PandeyPosted May 22, 2013, 1:00 AM
con.Open();
mark as answred if helpfull
Shankar MPosted May 22, 2013, 12:26 AM
Riddhi ValechaPosted May 21, 2013, 11:28 PM
Can you share your project ?? It's easier to make changes in the project....
Jignesh TrivediPosted May 21, 2013, 11:20 PM
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.
Sandeep Singh ShekhawatPosted May 21, 2013, 10:12 PM
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.