public bool insertStudent(int id, string fname, string lname, DateTime bdate, string gender, string phone, string address, MemoryStream picture) { SqlCommand cmd = new SqlCommand("insert into Std (id, fname,lname, bdate, gender, phone, address,picture)" + " values (@id, @fn, @ln, @bd, @gen, @phn, @add, @pic)", mydb.getConnection); cmd.Parameters.Add("@id", SqlDbType.Int).Value = id; cmd.Parameters.Add("@fn", SqlDbType.VarChar).Value = fname; cmd.Parameters.Add("@ln", SqlDbType.VarChar).Value = lname; cmd.Parameters.Add("@bd", SqlDbType.DateTime).Value = bdate; cmd.Parameters.Add("@gen", SqlDbType.VarChar).Value = gender; cmd.Parameters.Add("@phn", SqlDbType.VarChar).Value = phone; cmd.Parameters.Add("@add", SqlDbType.VarChar).Value = address; cmd.Parameters.Add("@pic", SqlDbType.Image).Value = picture.ToArray(); mydb.openConnection(); if (cmd.ExecuteNonQuery() == 1) { mydb.closeConnection(); return(true); } else { mydb.closeConnection(); return(false); } }
private void ButtonRegister_Click(object sender, EventArgs e) { if (chkAdmin.Checked == true) { MY_DB db = new MY_DB(); SqlCommand cmm = new SqlCommand("select * from Login", db.getConnection); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable(); da.SelectCommand = cmm; da.Fill(dt); int countRow = dt.Rows.Count; SqlCommand cmd = new SqlCommand("insert into Login (id, username, password)" + " values (@id, @user, @pass)", db.getConnection); cmd.Parameters.Add("@id", SqlDbType.Int).Value = countRow; cmd.Parameters.Add("@user", SqlDbType.VarChar).Value = txtUsername.Text; cmd.Parameters.Add("@pass", SqlDbType.VarChar).Value = txtPassword.Text; db.openConnection(); if (cmd.ExecuteNonQuery() == 1) { db.closeConnection(); } else { db.closeConnection(); } da.InsertCommand = cmd; da.Update(dt); MessageBox.Show("Sign up successful", "Adding...", MessageBoxButtons.OK, MessageBoxIcon.Information); } }