StudentInsertPage.aspx.cs
Dosyayı İndir
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using System.Data.SqlClient;
namespace WebProject
{
public partial class StudentInsertPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
string connectionString = "server=localhost;DataBase=CourseDatabase;uid=sa;pwd=godoro";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
String sql = "select * from School";
SqlCommand command = new SqlCommand(sql, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read()) {
int schoolId = (int)reader["SchoolId"];
string schoolName = (string)reader["SchoolName"];
ListItem schoolItem = new ListItem();
schoolItem.Text = schoolName;
schoolItem.Value = schoolId.ToString();
SchoolIdDropDownList.Items.Add(schoolItem);
}
reader.Close();
connection.Close();
}
}
protected void CancelButton_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected void OkButton_Click(object sender, EventArgs e)
{
string firstName = FirstNameTextBox.Text;
string lastName = LastNameTextBox.Text;
bool isGraduated = IsGraduatedCheckBox.Checked;
double bodyWeight = Double.Parse( BodyWeightTextBox.Text );
DateTime birthDate = BirhDateCalendar.SelectedDate;
string schoolIdString = SchoolIdDropDownList.SelectedValue;
string connectionString = "server=localhost;DataBase=CourseDatabase;uid=sa;pwd=godoro";
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
//string sql = "insert into Student(FirstName,LastName)"
// + " Values('" + firstName + "','" + lastName + "')";
string sql = "insert into Student(FirstName,LastName,BirthDate,IsGraduated,BodyWeight,SchoolId)"
+ " Values('" + firstName + "','" + lastName + "','" + birthDate.ToShortDateString() + "',"
+ (isGraduated ? 1 : 0) + "," + bodyWeight .ToString()+ "," + schoolIdString + ")";
SqlCommand command = new SqlCommand(sql, connection);
int affectedRowCount = command.ExecuteNonQuery();
if (affectedRowCount >= 0) {
MessageLabel.Text = "Öğrenci başarıyla eklenmiştir";
} else {
MessageLabel.Text = "Öğrenci eklerken hata oluştu";
}
connection.Close();
}
}
}
Dosyayı İndir