TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Nedzad Mesic
NA
13
555
How insert update record in c# with sql database and combobo
Dec 11 2017 8:50 AM
I have 2 table in my sql database:
1. city - id - cityname
2.Student -id -studentname -cityID
I successfully insert, edit and delete data in the database.
I have two c# windows form for City:
1.frmCity with dataGridView and all City data.... 2.frmAddEditCity with textbox for insert and edit....
I have two c# windows form for Student:
1.frmCity with dataGridView and all Studentdata.... 2.frmAddEditStudent with textbox for insert and edit...
BUT.....
In form frmAddEditStudent FOR CityID I have textbox and I use DataBindigs (BindigSource property) to get data... I can disply CITY id or City name...
My problem is when I want to insert new record or edit some record... and I don't know how to use combobox instead textbox "txtCityID" and save and edit record.
If I edit some record in combobox should be selected city name from that Student record(new york) with the possibility of changing through combox (and in combo are all citys from database)
is the same with insert - i need combobox with city names and value(id), and chois city from combo to save record...
This is my code for inser new record and edit record frmAddEditStudent:
public
partial
class
frmAddEditStudent : Form
{
tblStudentEntities dbIme;
CityEntities Citydb;
public
frmAddEditStudent(tblStudent obj)
{
InitializeComponent();
dbIme =
new
tblStudentEntities();
if
(obj ==
null
)
{
tblStudentBindingSource.DataSource =
new
tblStudent();
//Add contact to model, allow insert
dbIme.tblStudents.Add(tblStudentBindingSource.Current
as
tblStudent);
}
else
{
tblStudentBindingSource.DataSource = obj;
//Attach contact to model, allow edit
dbIme.tblStudents.Attach(tblStudentBindingSource.Current
as
tblStudent);
}
}
private
void
frmAddEditStudent_FormClosing(
object
sender, FormClosingEventArgs e)
{
if
(DialogResult == DialogResult.OK)
{
if
(
string
.IsNullOrEmpty(txtStudentName.Text))
{
MessageBox.Show(
"Please Insert data"
,
"Message"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
txtStudentName.Focus();
e.Cancel =
true
;
return
;
}
dbIme.SaveChanges();
e.Cancel =
false
;
}
e.Cancel =
false
;
}
private
void
frmAddEditStudent_Load(
object
sender, EventArgs e)
{
Citydb =
new
CityEntities();
tblCityBindingSource.DataSource = Citydb.tblCitys.ToList();
}
}
Reply
Answers (
2
)
Zlib decompression is not working on 64 bit configuration
Json data deserialize