ကြၽန္ေတာ္ သူငယ္ခ်င္းတို႔ရဲ႕ comment ထဲမွာ ေမးတဲ႔ ေမးခြန္းေလးေတြကို အလွ်င္းသင့္သလို ျပန္ေျဖနိုင္ေအာင္ဆိုျပီး Question and Answer ဆိုတဲ႔ အပို္င္းေလး ထည့္ဖို႔ စဥ္းစားမိတယ္။ ဒါနဲ႔ ဒီ category ေလးကို စလိုက္ပါတယ္။ သူငယ္ခ်င္းတို႔ C# နဲ႔ ပတ္သက္ျပီး သိခ်င္တာေလးေတြ ေမးနိုင္ပါတယ္။ ကြၽန္ေတာ္ သိသေလာက္ ေျဖေပးပါ့မယ္။ အဲ မသိရင္လည္း ၾကိဳးစား ရွာဖတ္ျပီး ေျဖေပးနိုင္ေအာင္ ၾကိဳးစားပါ့မယ္။
အရင္ဆံုး Ko Thet ေမးတဲ႔ ေမးခြန္းေလးနဲ႔ စလိုက္ရေအာင္….
Q : How to populate data in text box by using combo box.Says, Chose ID in Combo Box, then Name, Add, City, Contact no and etc will be shown in respective text box control. If you have time, please show me.
Comment by Ko Thet — October 22, 2009
A : ဒီေမးခြန္းေလးအတြက္ ကြၽန္ေတာ္ example program ေလး တစ္ခု ေရးျပပါမယ္။ အရင္ဆံုး ေအာက္မွာ ျပထားတဲ႔ ပံု(၁) အတိုင္း database ထဲမွာ table ေလး တစ္ခုေဆာက္လိုက္ပါတယ္။
Database name => Course_DB
Table name => EmployeeTable

pic_1
Note: EmployeeID ကို ကြၽန္ေတာ္တို႔ ထည့္စရာ မလိုပဲ database မွာ auto တိုးသြားခ်င္လို႔ အဲ့ဒီ column properties မွာ (Is Identity) ကို Yes လို႔ ေပးလိုက္ပါတယ္။ Identity Seed (1) ကေနစျပီး Identity Increment (1) စီ တိုးသြားမယ္လို႔ ေျပာတာပါ။
ျပီးရင္ အဲ့ဒီ EmployeeTable ထဲကို Sample Data နည္းနည္း ျဖည့္ေပးထားလိုက္ပါမယ္။ ပံု(၂) နဲ႔ ပံု(၃)မွာ ၾကည့္ပါ။

pic_2

pic_3
database ေဆာက္ျပီးေတာ့ အခုကြၽန္ေတာ္တို႔ program ဘက္အပိုင္းကို ဆက္ပါမယ္။ New Window Application တစ္ခုယူျပီး ပံု(၄) အတိုင္း design ခ်လိုက္ပါတယ္။

pic_4
Database ထဲက data ကို window form မွာ ခ်ိတ္ျပဖို႔အတြက္ ကြၽန္ေတာ္ အခု ပံု(၅) မွာျပထားသလို အဆင့္ဆင့္ လုပ္သြားလိုက္ပါ။

pic_5

pic_6
Data Source Configuration Wizard မွာ ‘DataBase’ ကိုေရြးျပီး Next ထပ္ႏွိပ္လိုက္ပါ။ ဒါဆိုရင္ ပံု(၇) မွာ ျပထားသလို database connection ေရြးခိုင္းတဲ႔ ေနရာကိုေရာက္ပါမယ္။ ကြၽန္ေတာ္တို႔က အခုမွ connection အသစ္ေဆာက္မွာ ျဖစ္လို႔ New Connection button ကိုႏွိပ္လိုက္ပါ။ Add connection wizard ေရာက္ရင္ ပံု(ဂ) မွာ ျပထားသလိုမ်ိဳး connection တစ္ခု ေဆာက္လိုက္ပါ။

pic_7

pic_8

pic_9
ပံု(၉) မွာ ျပထားတဲ႔ အတိုင္း ကြၽန္ေတာ္တို႔ database connection တစ္ခုရပါျပီ။ အဲ့ဒီ connection string ထဲမွာ password ထည့္သိမ္းထားမယ္ဆိုရင္ ဒုတိယ radio button ေလးကို ေရြးေပးရပါမယ္။

pic_10
ေနာက္တစ္ဆင့္မွာ connection setting ေလးကို သိမ္းမလားလို႔ ေမးပါတယ္။ အိုေက သိမ္းထားလိုက္ပါ… နာမည္ကိုလည္း မေျပာင္းတာေကာင္းပါတယ္။

pic_11
အခုကြၽန္ေတာ္တို႔ combobox ကေနတဆင့္ dataset ေဆာက္တဲ႔ အဆင္ျပီးသြားပါျပီ။ အခု ေျပာသလိုမေဆာက္ခ်င္ဘူး ဒါမွမဟုတ္လည္း ကြၽန္ေတာ္တို႔ form ထဲမွာ combobox မပါဘူးဆိုရင္လည္း dataset ၾကီးပဲ ဒီတိုင္း ယူျပီး ေဆာက္လို႔လည္း ရပါတယ္။ (Project Menu -> Add New Items -> DataSet )

pic_12
Code file ထဲမွာလည္း Form_Load event ကို auto ေဆာက္ေပးသြားပါလိမ့္မယ္။
private void Form1_Load(object sender, EventArgs e)
{
this.employeeTableTableAdapter.Fill(this.course_DBDataSet.EmployeeTable);
}
Database ထဲက data ေတြကို ကြၽန္ေတာ္တို႔ program ရဲ႕ memory ေပၚမွာ ယူျပီး သိမ္းေပးထားတဲ႔ အလုပ္ကို DataSet က လုပ္ေပးပါတယ္။ အဲ့ဒီ dataset ထဲကို data ေတြ ပို႔ေဆာင္ေပးတဲ႔ အလုပ္ကိုေတာ့ tableadapter က တာ၀န္ယူပါတယ္။ Form Load event ထဲမွာ ေရးေပးသြားတဲ႔ အလုပ္က အဲ့ဒီ data fill လုပ္ေပးတဲ႔ အလုပ္ပါ။ DataSet ထဲက data ေတြကို UI Controls ေတြမွာ ျပဖို႔ အတြက္ကိုေတာ့ BindingSource ကိုသံုးရပါတယ္။
pic_13
အခု program ေလးကို အခုေန run လိုက္ရင္ combobox ထဲမွာ employee no ေတြကိုေတြ႔ေနရမွာပါ။ ဒါေပမယ့္ textbox ေတြမွာေတာ့ ေပၚမွာ မဟုတ္ေသးပါဘူး။ ကြၽန္ေတာ္တို႔ textbox ေတြနဲ႔ မခ်ိတ္ေပးရေသးဘူးေလ။ အိုေက အခု textbox ေတြနဲ႔ bindingsource နဲ႔ ခ်ိတ္ေပးပါ့မယ္။

pic_14
Textbox နဲ႔ databind ဖို႔အတြက္ textbox ရဲ႕ properties window ကိုသြားလိုက္ပါ။ အေပၚဆံုးနားမွာ (DataBindings) ဆိုတဲ႔ property ေလးကို ဖြင့္လိုက္ျပီး၊ Text property ကိုႏွိပ္လိုက္ပါ။ အဲ့ဒီမွာ ကြၽန္ေတာ္တို႔ form မွာ ရွိေနတဲ႔ employeeTableBindingSource ကိုေတြရပါမယ္။ အဲ့ဒီ bindingsource ထဲကေန ကြၽန္ေတာ္တို႔ textbox မွာ ျပခ်င္တဲ႔ column name ကိုေရြးေပးလိုက္ရံုပါပဲ။ ကဲ… က်န္တဲ႔ textbox ေတြအားလံုးမွာလည္း ဒီတိုင္းပဲ လိုက္လုပ္လိုက္ပါ။ အခုဆိုရင္ေတာ့ Ko Thet လိုခ်င္တဲ႔ Program ပံုစံေလး ရျပီထင္ပါတယ္။
Excellent! I got it. More than a word of “Thank you so much”.
with best regards,
Comment by Ko Thet — October 24, 2009 @ 1:07 AM
Sorry, one more again. I have one form with 2 tabs. First tab is for datagrid view.The other is to insert data to same table.I have bound table to datagrid view of first tab. it is successful. The problem is that when I try to insert new data to same table from another tab after data are shown in datagrid view, it shows “sql connection can’t open”. But it is working if I do inserting without datagrid tab. I like to show you with screen shot and my code also. Could i get your email address. Thanks in advance.
Best Regards,
Comment by Ko Thet — November 7, 2009 @ 2:11 PM
sevenlamp@gmail.com ပါ
Comment by sevenlamp — November 8, 2009 @ 9:14 AM
can u give me ur gmail
i’m learning c#.net
your articles are excellent for me to learn
Comment by chan myae naing — December 13, 2010 @ 2:58 PM
please see @ http://www.sevenlamp.com
Comment by sevenlamp — December 14, 2010 @ 1:33 PM
7 Lamps Sir,
ကြ်န္ေတာ္ေမးစရာေလးရွိေနပါတယ္္။ကြ်န္ေတာ္ table တစ္ခု create လုပ္လိုက္တယ္ Student table ေပါ႔။ attribute အေနနဲ႔ StudentCode ပါတယ္ဗ်။ Student တစ္ေယာက္ကို student code နံပါတ္ထားရွိပါ႔မယ္… ဒါမယ္႔ ဥပမာ…. Form ရဲ႔ Text box ကေန “SN-00005″ လို႔ auto increase တိုးထည္႔ေပးခ်င္တယ္ဗ်ာ…student အသစ္သြင္းတိုင္းေပါ႔ေနာ္။
Save လုပ္လိုက္တာနဲ႔ Student table ရဲ႔ StudentCode (field) ထဲမွာသြား၀င္ေစခ်င္ပါတယ္ဗ်ာ။အဲဒီမွာ ေသခ်ာတာကေတာ႔ StudentCode ရဲ႔ maximum value နဲ႔စဥ္းစားၿပီး
ထိမ္းေပးရဖြယ္ရွိတယ္ထင္တယ္… increase တိုးဖို႔ဆိုရင္ေပါ႔ေနာ္…..။C# code နဲ႔ sample ၿပေပးနုိင္မလား ကို sevenlamp ခင္္ဗ်ာ….။ဟို…… အားလပ္လုိ႔ေရးခြင္႔ႀကံုမယ္႔အေနထားမွာေပါ႔ေနာ္။……… ဒါမယ္႔ မႀကာမႀကာ ေမွ်ာ္ေနမယ္ဗ်ာ ကြ်န္ေတာ္….ဟဲ။
Comment by Mg Thu — January 19, 2012 @ 2:13 PM