[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.7

GridView Update using ObjectDataSource

Asked by MadhuMenong in .NET Framework 3.x versions, .NET Framework 2.x, Programming for ASP.NET

Tags: ASP.Net GridView

Dear Friends,

I am using ASP.Net / GridView to list a number of cities.
Step one is to select the Country from a dropdownlist which is set to

AutoPostBack=true.

The GridView is being populated using an ObjectDataSource  and

ObjectDataTypeName details of which are given in the code.

Entity : CityEntity
DAO : CityDAO

The Issue:

In the edit mode the country is changed to a dropdownlist. I change the selected country and click the update button/link to save the changes.

But when the city table is updated the CountryName fields is left blank. The city table records both the CountryId and Country name so I need to pass both the Text and value of the DropDownList.

I tried to handle the onupdating event and changing the

e.InputParameters["objcity"] to include the country name in the CityEntity, but

getting the error InputParameters is a readonly field.

Is there a way in which:
1. With minimal change in code and without handling the OnUpdating event I can

pass the CountryName from the ddlCountry control. I need to pass both the

CountryID i.e ddlCountry.SelectedValue and the CountryName i.e

ddlCountry.SelectedItem.Text to be passed for update.

2. e.InputParameters is showing as readonly. But the microsoft portal

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasour

ceview.updating(VS.80).aspx the sample is adding Key / Value to the

e.InputParameters. Now how is it possible.
Am I missing something.

Please help.

All related code and table structure is given in the code.

Thank You
Madhu Menon
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
614:
615:
616:
617:
618:
619:
620:
621:
622:
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643:
644:
645:
646:
647:
648:
649:
650:
651:
652:
653:
654:
655:
656:
657:
658:
659:
660:
661:
662:
663:
664:
665:
666:
667:
668:
669:
670:
671:
672:
673:
674:
675:
676:
677:
678:
679:
680:
681:
682:
683:
684:
685:
686:
687:
688:
689:
690:
691:
692:
693:
694:
695:
696:
697:
698:
699:
700:
701:
702:
703:
704:
705:
706:
707:
708:
709:
710:
711:
712:
713:
714:
715:
716:
717:
718:
719:
720:
721:
722:
723:
724:
725:
726:
727:
728:
729:
730:
731:
732:
733:
734:
735:
736:
737:
738:
739:
740:
741:
742:
743:
744:
745:
746:
747:
748:
749:
750:
751:
752:
753:
754:
755:
756:
757:
758:
759:
760:
761:
762:
763:
764:
765:
766:
767:
768:
769:
770:
771:
772:
773:
774:
775:
776:
777:
778:
779:
780:
781:
782:
783:
784:
785:
786:
787:
788:
789:
790:
791:
792:
793:
794:
795:
796:
797:
798:
799:
800:
801:
802:
803:
804:
805:
806:
807:
808:
809:
810:
811:
812:
813:
814:
815:
816:
817:
818:
819:
820:
821:
822:
823:
824:
825:
826:
827:
828:
829:
830:
831:
832:
833:
834:
835:
836:
837:
838:
839:
840:
841:
842:
843:
844:
845:
846:
847:
848:
849:
850:
851:
852:
853:
854:
855:
856:
857:
858:
859:
860:
861:
862:
863:
864:
865:
866:
867:
868:
869:
870:
871:
872:
873:
874:
875:
876:
877:
878:
879:
880:
881:
882:
883:
884:
885:
886:
887:
888:
889:
890:
891:
892:
893:
894:
895:
896:
897:
898:
899:
900:
901:
902:
903:
904:
905:
906:
907:
908:
909:
910:
911:
912:
913:
914:
915:
916:
917:
918:
919:
920:
921:
922:
923:
924:
925:
926:
927:
928:
929:
930:
931:
932:
933:
934:
935:
936:
937:
938:
939:
940:
941:
942:
943:
944:
945:
946:
947:
948:
949:
950:
951:
952:
953:
954:
955:
956:
957:
958:
959:
960:
961:
962:
963:
964:
965:
966:
967:
968:
969:
970:
971:
972:
973:
974:
975:
976:
977:
978:
979:
980:
981:
982:
983:
984:
985:
986:
987:
988:
989:
990:
991:
992:
993:
994:
995:
996:
997:
998:
999:
1000:
1001:
1002:
1003:
1004:
1005:
1006:
1007:
1008:
1009:
1010:
1011:
1012:
1013:
1014:
1015:
1016:
1017:
1018:
1019:
1020:
1021:
1022:
1023:
1024:
1025:
1026:
1027:
1028:
1029:
1030:
1031:
1032:
1033:
1034:
1035:
1036:
1037:
1038:
1039:
1040:
1041:
1042:
1043:
1044:
1045:
1046:
1047:
Entity
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace GridView.City
{
    public class CityEntity
    {
        public enum city : int
        {
            AutoID = 0,
            Status = 1,
            CityID = 2,
            CityName = 3,
            CountryName = 4,
            CountryID = 5,
            TimeZone = 6,
            ModifiedBy = 7,
            ModifiedDate = 8
 
        }
        #region  Private Variable
 
        private int _AutoID = 0;
 
        private int _Status = 0;
 
        private int _CityID = 0;
 
        private string _CityName = "";
 
        private string _CountryName = "";
 
        private int _CountryID = 0;
 
        private string _TimeZone = "";
 
        private int _ModifiedBy = 0;
 
 
        private DateTime _ModifiedDate = System.DateTime.Now;
 
        #endregion Private Variable
 
 
        public int AutoID
        {
            get { return _AutoID; }
            set { _AutoID = value; }
        }
 
        public int Status
        {
            get { return _Status; }
            set { _Status = value; }
        }
 
 
        public int CityID
        {
            get { return _CityID; }
            set { _CityID = value; }
        }
 
        public string CityName
        {
            get { return _CityName; }
            set { _CityName = value; }
        }
 
        public string CountryName
        {
            get { return _CountryName; }
            set { _CountryName = value; }
        }
 
 
        public int CountryID
        {
            get { return _CountryID; }
            set { _CountryID = value; }
        }
 
 
 
 
        public string TimeZone
        {
            get { return _TimeZone; }
            set { _TimeZone = value; }
        }
 
 
        public int ModifiedBy
        {
            get { return _ModifiedBy; }
            set { _ModifiedBy = value; }
        }
 
 
        public System.DateTime ModifiedDate
        {
            get
            {
                return _ModifiedDate;
 
            }
            set
            {
                _ModifiedDate = value;
            }
        }
    }
}
 
 
_____________________________________________________
 
DAO
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
 
 
namespace GridView.City
{
    public class CityDAO
    {
        private int i = 0;
        private string feedbackMsg = string.Empty;
        private string connString = 
 
WebConfigurationManager.ConnectionStrings["PegasusConnectionString2"].ToString()
 
;
        Parameter ObjPara = new Parameter();
        CommonClass ObjCom = new CommonClass();
        SqlConnection con;
        SqlCommand cmd;
        DataTable dt;
        private DataSet ds;
        private SqlDataAdapter da;
 
        public string Add(CityEntity objcity)
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                cmd = new SqlCommand(SysConstants.SP_CityCreate, con);
                cmd.CommandType = CommandType.StoredProcedure;
                //cmd.Parameters.Add(cAutoID, SqlDbType.Int).Value = 
 
objchain.AutoID;  
                cmd.Parameters.Add(ObjPara.GetParam(SysConstants.CityIDAt, 
 
ObjCom.GetId(SysConstants.CityID), ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam(SysConstants.CityNameAt, 
 
objcity.CityName, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@CountryName", 
 
objcity.CountryName, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@CountryID", 
 
objcity.CountryID, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam(SysConstants.TimeZoneAt, 
 
objcity.TimeZone, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@ModifiedBy", 
 
objcity.ModifiedBy, ParameterDirection.Input));
 
 
 
                i = cmd.ExecuteNonQuery();
 
 
                if (i == -1)
                {
                    feedbackMsg = 
 
WebConfigurationManager.AppSettings["Add"].ToString();
                    return feedbackMsg;
                }
                else
                {
                    feedbackMsg = 
 
WebConfigurationManager.AppSettings["add_failure"].ToString();
                    return feedbackMsg;
                }
 
            }
            catch (Exception e)
            {
                feedbackMsg = 
 
WebConfigurationManager.AppSettings["add_failure"].ToString() + 
 
e.Message.ToString();
                return feedbackMsg;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
 
        public string Update(CityEntity objcity)
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                cmd = new SqlCommand("SP_CityUpdate", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(ObjPara.GetParam("@AutoID", objcity.AutoID, 
 
ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@CityID", objcity.CityID, 
 
ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@CityName", 
 
objcity.CityName, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@CountryName", 
 
objcity.CountryName, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@CountryID", 
 
objcity.CountryID, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@TimeZone", 
 
objcity.TimeZone, ParameterDirection.Input));
                cmd.Parameters.Add(ObjPara.GetParam("@ModifiedBy", 
 
objcity.ModifiedBy, ParameterDirection.Input));
 
                i = cmd.ExecuteNonQuery();
 
 
                if (i == -1)
                {
                    feedbackMsg = "Successfully Updated!!!";
                    return feedbackMsg;
                }
                else
                {
                    feedbackMsg = "Update Failed!!!";
                    return feedbackMsg;
                }
 
            }
            catch (Exception e)
            {
                feedbackMsg = "Update Failed";
                return feedbackMsg;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
        public string Delete(int CityID)
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                cmd = new SqlCommand("SP_CityDelete", con);
                cmd.CommandType = CommandType.StoredProcedure;
 
                cmd.Parameters.Add(ObjPara.GetParam("@CityID", CityID, 
 
ParameterDirection.Input));
 
                i = cmd.ExecuteNonQuery(); //Working but getting correct values
                if (i == -1)
                {
                    feedbackMsg = 
 
WebConfigurationManager.AppSettings["Delete"].ToString();
                    return feedbackMsg;
                }
                else
                {
                    feedbackMsg = 
 
WebConfigurationManager.AppSettings["Delete_failure"].ToString();
                    return feedbackMsg;
                }
 
            }
            catch (Exception e)
            {
                feedbackMsg = 
 
WebConfigurationManager.AppSettings["delete_failure"].ToString() + 
 
e.Message.ToString();
                return feedbackMsg;
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
 
        public CityEntity getData(int CityID)
        {
            CityEntity city = new CityEntity();
 
            try
            {
                SqlDataReader dr;
                con = new SqlConnection(connString);
                con.Open();
                cmd = new SqlCommand("SP_CityGetData", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(ObjPara.GetParam("@CityID", CityID, 
 
ParameterDirection.Input));
 
                dr = cmd.ExecuteReader();
 
                if (dr != null)
                {
                    while (dr.Read())
                    {
                        if (dr.HasRows)
                        {
                            city.AutoID = (int)dr["AutoID"];
                            city.CityID = (int)dr["CityID"];
                            city.CityName = dr["CityName"].ToString();
                            city.CountryID = (int)dr["CountryID"];
                            city.CountryName = dr["CountryName"].ToString();
                            city.TimeZone = dr["TimeZone"].ToString();
                            city.ModifiedBy = (int)dr["ModifiedBy"];
                        }
                    }
 
                    dr.Dispose();
                }
                return city;
 
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
 
        public List<CityEntity> GetList()
        {
 
            try
            {
                SqlDataReader dr;
                List<CityEntity> ListCity = new List<CityEntity>();
                con = new SqlConnection(connString);
                cmd = new SqlCommand("SP_CityGetAllData", con);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
 
                    if (dr.HasRows)
                    {
                        CityEntity city = new CityEntity();
                        city.AutoID = (int)dr["AutoID"];
                        city.CityID = (int)dr["CityID"];
                        city.CityName = dr["CityName"].ToString();
                        city.CountryID = (int)dr["CountryID"];
                        city.TimeZone = dr["TimeZone"].ToString();
                        city.ModifiedBy = (int)dr["ModifiedBy"];
                        ListCity.Add(city);
                    }
 
 
                }
                return ListCity;
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
                cmd.Dispose();
                con.Dispose();
            }
 
        }
 
        public Hashtable SearchData(String Searchwhat)
        {
 
            try
            {
                Hashtable ht = new Hashtable();
                SqlDataReader dr;
                con = new SqlConnection(connString);
                con.Open();
                cmd = new SqlCommand("SP_CitySearchData", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(ObjPara.GetParam("@SearchOn", Searchwhat, 
 
ParameterDirection.Input));
 
 
                dr = cmd.ExecuteReader();
 
                if (dr != null)
                {
                    while (dr.Read())
                    {
                        if (dr.HasRows)
                        {
 
                            ht.Add(dr["CityName"], dr["CityID"]);
                        }
 
                    }
 
                    dr.Dispose();
                }
                return ht;
            }
            catch (Exception ex)
            {
 
 
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        }
 
 
        public DataTable GridSearch(string strcity, string strcountry, int 
 
pagesize, int currentpage)
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                dt = new DataTable();
                cmd = new SqlCommand("SP_CityLookup", con);
                cmd.CommandType = CommandType.StoredProcedure;
 
                cmd.Parameters.Add("@city_prefix", SqlDbType.VarChar);
                cmd.Parameters[0].Value = strcity;
 
                cmd.Parameters.Add("@country_prefix", SqlDbType.VarChar);
                cmd.Parameters[1].Value = strcountry;
 
                cmd.Parameters.Add("@PageSize", SqlDbType.Int);
                cmd.Parameters[2].Value = pagesize;
 
                cmd.Parameters.Add("@CurrentPage", SqlDbType.Int);
                cmd.Parameters[3].Value = currentpage;
 
                SqlParameter parm = new SqlParameter();
 
                parm = cmd.Parameters.Add("@PageCount", SqlDbType.Int);
                parm.Direction = ParameterDirection.Output;
 
                da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
            return dt;
        }
 
        public DataTable GridSearchLookup(string strcity, int pagesize, int 
 
currentpage, int countryID, string countryName, ref int TotalRows)
        {
            try
            {
                con = new SqlConnection(connString);
 
                dt = new DataTable();
                cmd = new SqlCommand("SP_CityLookupByCountry", con);
                cmd.CommandType = CommandType.StoredProcedure;
 
                cmd.Parameters.Add("@Prefix", SqlDbType.VarChar);
                cmd.Parameters[0].Value = strcity;
 
 
                cmd.Parameters.Add("@PageSize", SqlDbType.Int);
                cmd.Parameters[1].Value = pagesize;
 
                cmd.Parameters.Add("@CurrentPage", SqlDbType.Int);
                cmd.Parameters[2].Value = currentpage;
 
                cmd.Parameters.Add("@CountryID", SqlDbType.Int);
                cmd.Parameters[3].Value = countryID;
                cmd.Parameters.Add("@CountryName", SqlDbType.VarChar);
                cmd.Parameters[4].Value = countryName;
 
                SqlParameter parm = new SqlParameter();
 
                parm = cmd.Parameters.Add("@PageCount", SqlDbType.Int);
                parm.Direction = ParameterDirection.Output;
 
                con.Open();
 
                da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
 
                TotalRows = (int)cmd.Parameters["@PageCount"].Value;
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
            return dt;
        }
 
 
 
 
        public DataSet FetchByCityID(int CityID)
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                ds = new DataSet();
 
                cmd = new SqlCommand("SP_CityGetData", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(ObjPara.GetParam("@CityID", CityID, 
 
ParameterDirection.Input));
 
                da = new SqlDataAdapter(cmd);
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            return ds;
        }
 
 
        //to fill the country dropdown in city form
        public DataSet FillCountryList()
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                ds = new DataSet();
                da = new SqlDataAdapter("SP_CountryDisplay", con);
                da.Fill(ds, "Country");
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            return ds;
        }
 
        //end by dipti of cityform
 
        //fill city dropdownlist--dipti
        public DataSet FillCityList()
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                ds = new DataSet();
                da = new SqlDataAdapter("SP_CityDisplay", con);
                da.Fill(ds, "City");
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            return ds;
        }
 
        //amit work
        public DataSet GetCitiesByCountrySP(int countryID)
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                ds = new DataSet();
                SqlCommand cmd = new 
 
SqlCommand(SysConstants.SP_CitySelectedIndexDisplay, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(ObjPara.GetParam(SysConstants.CountryIDAt, 
 
countryID, ParameterDirection.Input));
                SqlDataAdapter da = new SqlDataAdapter(cmd);
 
                da.Fill(ds, "City");
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            return ds;
        }
 
        public DataSet GetCitiesByCountry(int countryID)
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                ds = new DataSet();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "Select [AutoID],	
 
[CityID],[CityName],[CountryID],[CountryName],[TimeZone],[ModifiedBy],[ModifiedD
 
ate] from tblCity Where CountryID = " + countryID.ToString() + " Order by 
 
CityName";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
          
                SqlDataAdapter da = new SqlDataAdapter(cmd);
 
                da.Fill(ds, "City");
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            return ds;
        }
        //end amit
        //narendar work
 
        public DataTable GridSearch(string strcity, int pagesize, int 
 
currentpage, ref int TotalRows)
        {
            try
            {
                con = new SqlConnection(connString);
 
                dt = new DataTable();
                cmd = new SqlCommand("SP_CityLookup", con);
                cmd.CommandType = CommandType.StoredProcedure;
 
                cmd.Parameters.Add("@Prefix", SqlDbType.VarChar);
                cmd.Parameters[0].Value = strcity;
 
 
                cmd.Parameters.Add("@PageSize", SqlDbType.Int);
                cmd.Parameters[1].Value = pagesize;
 
                cmd.Parameters.Add("@CurrentPage", SqlDbType.Int);
                cmd.Parameters[2].Value = currentpage;
 
                SqlParameter parm = new SqlParameter();
 
                parm = cmd.Parameters.Add("@PageCount", SqlDbType.Int);
                parm.Direction = ParameterDirection.Output;
 
                con.Open();
 
                da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(dt);
 
                TotalRows = (int)cmd.Parameters["@PageCount"].Value;
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
            return dt;
        }
 
        public DataTable Fetch()
        {
            try
            {
                con = new SqlConnection(connString);
                con.Open();
                dt = new DataTable();
                da = new SqlDataAdapter(SysConstants.SP_CityGetAllData, con);
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
            return dt;
        }
 
 
    }
}
 
___________________________________________________________
 
ASPX
 
<%@ Page Language="C#" AutoEventWireup="true" 
 
CodeBehind="GridView_InsertUpdateDelete.aspx.cs" 
 
Inherits="MadhuASPTutorial.ObjectDataSourceGridView.GridView_InsertUpdateDelete" 
 
%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ObjectDataSource ID="odsCountry" runat="server" 
            TypeName="GridView.City.CityDAO" 
 
SelectMethod="FillCountryList"></asp:ObjectDataSource>
            
    <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountry" 
    DataTextField="CountryName" DataValueField="CountryID" 
 
AutoPostBack="true"></asp:DropDownList>
    
    <br />
    <br />
    
    <asp:ObjectDataSource ID="odsCity" runat="server" 
 
TypeName="GridView.City.CityDAO" 
            DataObjectTypeName="GridView.City.CityEntity" DeleteMethod="Delete" 
            InsertMethod="Add" SelectMethod="GetCitiesByCountry" 
 
UpdateMethod="Update" 
            onupdating="odsCity_Updating" 
            OldValuesParameterFormatString="original_{0}">
        <DeleteParameters>
            <asp:Parameter Name="CityID" Type="Int32" />
        </DeleteParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlCountry" DefaultValue="0" 
 
Name="countryID" 
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
     </asp:ObjectDataSource>
    
    <asp:GridView ID="gvCities" runat="server" DataSourceID="odsCity" 
            DataKeyNames="CityID" AllowPaging="True"
     AutoGenerateColumns="False">
     <Columns>
         <asp:CommandField ShowEditButton="true" />
         <asp:TemplateField HeaderText="AutoID">
         <ItemTemplate> <asp:Label ID="lblAutoID" runat="server" Text='<%# 
 
Eval("AutoID") %>' ></asp:Label></ItemTemplate>
          <EditItemTemplate><asp:Label ID="lblAutoIDE" runat="server" Text='<%# 
 
Bind("AutoID") %>' ></asp:Label></EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="CityID">
         <ItemTemplate> <asp:Label ID="lblCityID" runat="server" Text='<%# 
 
Eval("CityID") %>' ></asp:Label></ItemTemplate>
         <EditItemTemplate> <asp:Label ID="lblCityIDE" runat="server" Text='<%# 
 
Bind("CityID") %>' ></asp:Label></EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="CityName">
         <ItemTemplate> <asp:Label ID="lblCityName" runat="server" Text='<%# 
 
Eval("CityName") %>' ></asp:Label></ItemTemplate>
         <EditItemTemplate> <asp:TextBox ID="txtCityName" runat="server" 
 
Text='<%# Bind("CityName") %>' ></asp:TextBox>
         <asp:RequiredFieldValidator ID="rfvCityName" runat="server" 
 
ControlToValidate="txtCityName" ErrorMessage="City Name is Required!!!" 
 
Display="Static">*</asp:RequiredFieldValidator>
         </EditItemTemplate>
         </asp:TemplateField>
         
         <asp:TemplateField HeaderText="CountryName">
         <ItemTemplate> <asp:Label ID="lblCountryName" runat="server" Text='<%# 
 
Eval("CountryName") %>' ></asp:Label></ItemTemplate>
         
         <EditItemTemplate> <asp:DropDownList ID="ddlCountry" runat="server" 
 
DataSourceID="odsCountry1" DataTextField="CountryName" 
 
DataValueField="CountryID" SelectedValue='<%# Bind("CountryID") %>' >
                <asp:ListItem Value="-1" Text="Select"></asp:ListItem>
         </asp:DropDownList>
         <asp:RequiredFieldValidator ID="rfvCountry" runat="server" 
 
ControlToValidate="ddlCountry" InitialValue="-1" ErrorMessage="Country should be 
 
selelcted!!!" Display="Static">*</asp:RequiredFieldValidator>
         <asp:ObjectDataSource ID="odsCountry1" runat="server" 
 
TypeName="GridView.City.CityDAO" 
 
SelectMethod="FillCountryList"></asp:ObjectDataSource>
         </EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="TimeZone">
         <ItemTemplate> <asp:Label ID="lblTimeZone" runat="server" Text='<%# 
 
Eval("TimeZone") %>' ></asp:Label></ItemTemplate>
         <EditItemTemplate> <asp:TextBox ID="txtTimeZone" runat="server" 
 
Text='<%# Bind("TimeZone") %>' ></asp:TextBox></EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Modified By">
         <ItemTemplate> <asp:Label ID="lblModifiedBy" runat="server" Text='<%# 
 
Eval("ModifiedBy") %>' ></asp:Label></ItemTemplate>
         <EditItemTemplate> <asp:TextBox ID="txtAModifiedBy" runat="server" 
 
Text='<%# Bind("ModifiedBy") %>'></asp:TextBox></EditItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Modified Date">
         <ItemTemplate> <asp:Label ID="lblModifiedDate" runat="server" Text='<%# 
 
Eval("ModifiedDate") %>' ></asp:Label></ItemTemplate>
         <EditItemTemplate> <asp:TextBox ID="txtModifiedDate" runat="server" 
 
Text='<%# Bind("ModifiedDate") %>' ></asp:TextBox></EditItemTemplate>
         </asp:TemplateField>
                  
     </Columns>
     
     
     </asp:GridView>
    
    </div>
    </form>
</body>
</html>
 
 
_________________________________________________________
 
ASPX.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace MadhuASPTutorial.ObjectDataSourceGridView
{
    public partial class GridView_InsertUpdateDelete : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void odsCity_Updating(object sender, 
 
ObjectDataSourceMethodEventArgs e)
        {
            GridView.City.CityEntity city = new GridView.City.CityEntity();
 
            // The names of the parameters are the same as
            // the variable names for the method that is invoked to
            // perform the Update. The InputParameters collection is
            // an IDictionary collection of name/value pairs,
            // not a ParameterCollection.
 
            city = (GridView.City.CityEntity)e.InputParameters["objcity"];
 
            city.CountryName = ddlCountry.SelectedItem.Text;
            //e.InputParameters.Remove("objcity");
       
           // e.InputParameters.Add("objcity", city);
           
            city = null;
 
           
           
        }
    }
}
 
_________________________________________________
 
Table Structure
 
CREATE TABLE [dbo].[tblCountry](
	[AutoID] [int] IDENTITY(1,1) NOT NULL,
	[CountryID] [int] NOT NULL,
	[CountryName] [varchar](100) NOT NULL,
	[CountryCode] [varchar](2) NULL,
	[RegionName] [varchar](100) NULL,
	[RegionID] [int] NULL,
	[ISD] [nchar](10) NULL,
	[CurrencyCode] [varchar](10) NULL,
	[CurrencyName] [nvarchar](100) NULL,
	[TimeZone] [nchar](100) NULL,
	[ModifiedBy] [int] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL
) ON [PRIMARY]
 
 
CREATE TABLE [dbo].[tblCity](
	[AutoID] [int] IDENTITY(1,1) NOT NULL,
	[CityID] [int] NULL,
	[CityName] [nvarchar](100) NULL,
	[CountryID] [int] NULL,
	[CountryName] [nvarchar](100) NULL,
	[TimeZone] [nchar](10) NULL,
	[ModifiedBy] [int] NULL,
	[ModifiedDate] [datetime] NULL
) ON [PRIMARY]
 
 
_________________________________
 
Sample data
 
Country
 
11,110,Afghanistan,,Asia,12,93        ,AFN,Afghanistan Afghanis,                 
 
                                                                                 
 
  ,0,2009-06-28 03:13:37.500000000
12,111,Albania,,Europe,5,355       ,ALL,Albania Leke,                            
 
                                                                        
 
,0,2009-06-28 03:17:27.577000000
 
 
City
 
 
8222,8291,Mazar-I-Sharif,110,Afghanistan,          ,100,2009-03-17 
 
20:50:29.080000000
8224,8293,Herat,111,Afghanistan,          ,100,2009-08-20 01:06:52.700000000
8226,8295,Kabul,110,Afghanistan,          ,100,2009-03-17 20:50:29.080000000
8228,8297,Kandahar,110,Afghanistan,          ,100,2009-03-17 20:50:29.080000000
8230,8299,Tirana,111,Albania,          ,100,2009-03-17 20:50:29.080000000
 
Related Solutions
Keywords: GridView Update using ObjectDataSou…
 
Loading Advertisement...
 
[+][-]09/11/09 07:29 PM, ID: 25314809Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: .NET Framework 3.x versions, .NET Framework 2.x, Programming for ASP.NET
Tags: ASP.Net GridView
Sign Up Now!
Solution Provided By: KPMT-Technician
Participating Experts: 1
Solution Grade: A
 
[+][-]08/29/09 05:29 PM, ID: 25215930Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/31/09 04:30 AM, ID: 25221946Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/31/09 06:20 AM, ID: 25222512Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/01/09 05:27 AM, ID: 25230810Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/01/09 05:55 AM, ID: 25231032Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/01/09 10:58 PM, ID: 25238271Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/02/09 11:00 PM, ID: 25248165Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/02/09 11:17 PM, ID: 25248212Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625