In my Project I have student database and two webform. In database there are 6 columns( Email, Password, Name, Phone, ExamStatus, Score. I am inserting Email and Password value through webform1 and Name and password through another webform. Actually webform1 redirects to webform2.
here is my code behind file of webform1:
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;
public partial class signin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void signupbtn_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Punam\\Desktop\\Project\\App_Data\\OnlineLearning.mdf;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("insert into Student (Email, Password) values(@email, @password)", con );
cmd.Parameters.AddWithValue("@email", txtboxemail.Text);
cmd.Parameters.AddWithValue("@password", txtboxpass.Text);
con.Open();
cmd.ExecuteNonQuery();
Session["signup"] = txtboxemail.Text.ToString();
Response.Redirect("profile.aspx");
}
}
}
and codebehind file of webform2:
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;
public partial class profile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["signup"] == null)
{
Response.Redirect("signup.aspx");
}
}
protected void btnsave_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Punam\\Desktop\\Project\\App_Data\\OnlineLearning.mdf;Integrated Security=True"))
{
string mail = Session["signup"].ToString();
SqlCommand cmd = new SqlCommand("Update Student set Name = @name, Phone = @phoneno where Email = + mail ", con);
cmd.Parameters.AddWithValue("@name", txtboxname.Text);
cmd.Parameters.AddWithValue("@phoneno", txtboxphone.Text);
con.Open();
cmd.ExecuteNonQuery();
Response.Redirect("login.aspx");
}
}
}
But it is throwing error System.Data.SqlClient.SqlException: Invalid column name 'mail'.