CloseConnection









How do I use Close Connection
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PhotonConverter.cs Copy
189     static void ConvertToPhotonAPI(string file)
190     {
191         string text = File.ReadAllText(file);
192
193         bool isJS = file.Contains(".js");
194
195         file = file.Replace("\\", "/"); // Get Class name for JS
196         string className = file.Substring(file.LastIndexOf("/")+1);
197         className = className.Substring(0, className.IndexOf("."));
198
199
200         //REGEXP STUFF
201         //Valid are: Space { } , /n /r
202         //string NOT_VAR = @"([^A-Za-z0-9_\[\]\.]+)";
203         string NOT_VAR_WITH_DOT = @"([^A-Za-z0-9_]+)";
204
205         //string VAR_NONARRAY = @"[^A-Za-z0-9_]";
206
207
208         //NetworkView
209         {
210             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkView" + NOT_VAR_WITH_DOT, "$1PhotonView$2");
211             text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
212             text = PregReplace(text, NOT_VAR_WITH_DOT + "stateSynchronization" + NOT_VAR_WITH_DOT, "$1synchronization$2");
213             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkStateSynchronization" + NOT_VAR_WITH_DOT, "$1ViewSynchronization$2"); // map Unity enum to ours
214             //.RPC
215             text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode.Server" + NOT_VAR_WITH_DOT, "$1PhotonTargets.MasterClient$2");
216             text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode" + NOT_VAR_WITH_DOT, "$1PhotonTargets$2");
217         }
218
219         //NetworkMessageInfo: 100%
220         {
221             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkMessageInfo" + NOT_VAR_WITH_DOT, "$1PhotonMessageInfo$2");
222             text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
223         }
224
225         //NetworkViewID:
226         {
227             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkViewID" + NOT_VAR_WITH_DOT, "$1int$2"); //We simply use an int
228         }
229
230         //NetworkPlayer
231         {
232             text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPlayer" + NOT_VAR_WITH_DOT, "$1PhotonPlayer$2");
233         }
234
235         //Network
236         {
237             //Monobehaviour callbacks
238             {
239                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerConnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerConnected$2");
240                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerDisconnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerDisconnected$2");
241                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnNetworkInstantiate" + NOT_VAR_WITH_DOT, "$1OnPhotonInstantiate$2");
242                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnSerializeNetworkView" + NOT_VAR_WITH_DOT, "$1OnPhotonSerializeView$2");
243                 text = PregReplace(text, NOT_VAR_WITH_DOT + "BitStream" + NOT_VAR_WITH_DOT, "$1PhotonStream$2");
244
245                 //Not completely the same meaning
246                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnServerInitialized" + NOT_VAR_WITH_DOT, "$1OnCreatedRoom$2");
247                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnConnectedToServer" + NOT_VAR_WITH_DOT, "$1OnJoinedRoom$2");
248
249                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnectToMasterServer" + NOT_VAR_WITH_DOT, "$1OnFailedToConnectToPhoton$2");
250                 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnect" + NOT_VAR_WITH_DOT, "$1OnFailedToConnect_OBSELETE$2");
251             }
252
253             //Variables
254             {
255
256                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.connections" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList$2");
257                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
258                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isClient" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
259
260                 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPeerType" + NOT_VAR_WITH_DOT, "$1ConnectionState$2");
261                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.peerType" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.connectionState$2");
262                 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Server" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
263                 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Client" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
264                 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.playerList.Length" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList.Count$2");
265
266                 /*DROPPED:
267                     minimumAllocatableViewIDs
268                     natFacilitatorIP is dropped
269                     natFacilitatorPort is dropped
270                     connectionTesterIP
271                     connectionTesterPort
272                     proxyIP
273                     proxyPort
274                     useProxy
275                     proxyPassword
276                  */
277             }
278
279             //Methods
280             {
281                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.InitializeServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.CreateRoom$2");
282                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.Connect" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.JoinRoom$2");
283                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetAveragePing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
284                 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetLastPing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
285                 /*DROPPED:
286                     TestConnection
287                     TestConnectionNAT
288                     HavePublicAddress
289                 */
290             }
291
292             //Overall
293             text = PregReplace(text, NOT_VAR_WITH_DOT + "Network" + NOT_VAR_WITH_DOT, "$1PhotonNetwork$2");
294
295
296         //Changed methods
297              string ignoreMe = @"([A-Za-z0-9_\[\]\(\) ]+)";
298
299          text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.GetPing\\(" + ignoreMe+"\\);", "$1PhotonNetwork.GetPing();");
300         text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.CloseConnection\\(" + ignoreMe+","+ignoreMe+"\\);", "$1PhotonNetwork.CloseConnection($2);");
301
302         }
303
304         //General
305         {
306             if (text.Contains("Photon")) //Only use the PhotonMonoBehaviour if we use photonView and friends.
307             {
308                 if (isJS)//JS
309                 {
310                     if (text.Contains("extends MonoBehaviour"))
311                         text = PregReplace(text, "extends MonoBehaviour", "extends Photon.MonoBehaviour");
312                     else
313                         text = "class " + className + " extends Photon.MonoBehaviour {\n" + text + "\n}";
314                 }
315                 else //C#
316                     text = PregReplace(text, ": MonoBehaviour", ": Photon.MonoBehaviour");
317             }
318         }
319
320         File.WriteAllText(file, text);
321     }
File name: NetworkingPeer.cs Copy
1631     public void OnEvent(EventData photonEvent)
1632     {
1633         if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
1634             Debug.Log(string.Format("OnEvent: {0}", photonEvent.ToString()));
1635
1636         int actorNr = -1;
1637         PhotonPlayer originatingPlayer = null;
1638
1639         if (photonEvent.Parameters.ContainsKey(ParameterCode.ActorNr))
1640         {
1641             actorNr = (int)photonEvent[ParameterCode.ActorNr];
1642             if (this.mActors.ContainsKey(actorNr))
1643             {
1644                 originatingPlayer = (PhotonPlayer)this.mActors[actorNr];
1645             }
1646             //else
1647             //{
1648             // // the actor sending this event is not in actorlist. this is usually no problem
1649             // if (photonEvent.Code != (byte)LiteOpCode.Join)
1650             // {
1651             // Debug.LogWarning("Received event, but we do not have this actor: " + actorNr);
1652             // }
1653             //}
1654         }
1655
1656         switch (photonEvent.Code)
1657         {
1658             case PunEvent.OwnershipRequest:
1659             {
1660                 int[] requestValues = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
1661                 int requestedViewId = requestValues[0];
1662                 int currentOwner = requestValues[1];
1663                 Debug.Log("Ev OwnershipRequest: " + photonEvent.Parameters.ToStringFull() + " ViewID: " + requestedViewId + " from: " + currentOwner + " Time: " + Environment.TickCount%1000);
1664
1665                 PhotonView requestedView = PhotonView.Find(requestedViewId);
1666                 if (requestedView == null)
1667                 {
1668                     Debug.LogWarning("Can't find PhotonView of incoming OwnershipRequest. ViewId not found: " + requestedViewId);
1669                     break;
1670                 }
1671
1672                 Debug.Log("Ev OwnershipRequest PhotonView.ownershipTransfer: " + requestedView.ownershipTransfer + " .ownerId: " + requestedView.ownerId + " isOwnerActive: " + requestedView.isOwnerActive + ". This client's player: " + PhotonNetwork.player.ToStringFull());
1673
1674                 switch (requestedView.ownershipTransfer)
1675                 {
1676                     case OwnershipOption.Fixed:
1677                         Debug.LogWarning("Ownership mode == fixed. Ignoring request.");
1678                         break;
1679                     case OwnershipOption.Takeover:
1680                         if (currentOwner == requestedView.ownerId)
1681                         {
1682                             // a takeover is successful automatically, if taken from current owner
1683                             requestedView.ownerId = actorNr;
1684                         }
1685                         break;
1686                     case OwnershipOption.Request:
1687                         if (currentOwner == PhotonNetwork.player.ID || PhotonNetwork.player.isMasterClient)
1688                         {
1689                             if ((requestedView.ownerId == PhotonNetwork.player.ID) || (PhotonNetwork.player.isMasterClient && !requestedView.isOwnerActive))
1690                             {
1691                                 SendMonoMessage(PhotonNetworkingMessage.OnOwnershipRequest, new object[] {requestedView, originatingPlayer});
1692                             }
1693                         }
1694                         break;
1695                     default:
1696                         break;
1697                 }
1698             }
1699                 break;
1700
1701             case PunEvent.OwnershipTransfer:
1702                 {
1703                     int[] transferViewToUserID = (int[]) photonEvent.Parameters[ParameterCode.CustomEventContent];
1704                     Debug.Log("Ev OwnershipTransfer. ViewID " + transferViewToUserID[0] + " to: " + transferViewToUserID[1] + " Time: " + Environment.TickCount%1000);
1705
1706                     int requestedViewId = transferViewToUserID[0];
1707                     int newOwnerId = transferViewToUserID[1];
1708
1709                     PhotonView pv = PhotonView.Find(requestedViewId);
1710                     pv.ownerId = newOwnerId;
1711
1712                     break;
1713                 }
1714             case EventCode.GameList:
1715                 {
1716                     this.mGameList = new Dictionary();
1717                     Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
1718                     foreach (DictionaryEntry game in games)
1719                     {
1720                         string gameName = (string)game.Key;
1721                         this.mGameList[gameName] = new RoomInfo(gameName, (Hashtable)game.Value);
1722                     }
1723                     mGameListCopy = new RoomInfo[mGameList.Count];
1724                     mGameList.Values.CopyTo(mGameListCopy, 0);
1725                     SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
1726                     break;
1727                 }
1728
1729             case EventCode.GameListUpdate:
1730                 {
1731                     Hashtable games = (Hashtable)photonEvent[ParameterCode.GameList];
1732                     foreach (DictionaryEntry room in games)
1733                     {
1734                         string gameName = (string)room.Key;
1735                         RoomInfo game = new RoomInfo(gameName, (Hashtable)room.Value);
1736                         if (game.removedFromList)
1737                         {
1738                             this.mGameList.Remove(gameName);
1739                         }
1740                         else
1741                         {
1742                             this.mGameList[gameName] = game;
1743                         }
1744                     }
1745                     this.mGameListCopy = new RoomInfo[this.mGameList.Count];
1746                     this.mGameList.Values.CopyTo(this.mGameListCopy, 0);
1747                     SendMonoMessage(PhotonNetworkingMessage.OnReceivedRoomListUpdate);
1748                     break;
1749                 }
1750
1751             case EventCode.QueueState:
1752                 // not used anymore
1753                 break;
1754
1755             case EventCode.AppStats:
1756                 // Debug.LogInfo("Received stats!");
1757                 this.mPlayersInRoomsCount = (int)photonEvent[ParameterCode.PeerCount];
1758                 this.mPlayersOnMasterCount = (int)photonEvent[ParameterCode.MasterPeerCount];
1759                 this.mGameCount = (int)photonEvent[ParameterCode.GameCount];
1760                 break;
1761
1762             case EventCode.Join:
1763                 // actorNr is fetched out of event above
1764                 Hashtable actorProperties = (Hashtable)photonEvent[ParameterCode.PlayerProperties];
1765                 if (originatingPlayer == null)
1766                 {
1767                     bool isLocal = this.mLocalActor.ID == actorNr;
1768                     this.AddNewPlayer(actorNr, new PhotonPlayer(isLocal, actorNr, actorProperties));
1769                     this.ResetPhotonViewsOnSerialize(); // This sets the correct OnSerializeState for Reliable OnSerialize
1770                 }
1771
1772                 if (actorNr == this.mLocalActor.ID)
1773                 {
1774                     // in this player's 'own' join event, we get a complete list of players in the room, so check if we know all players
1775                     int[] actorsInRoom = (int[])photonEvent[ParameterCode.ActorList];
1776                     foreach (int actorNrToCheck in actorsInRoom)
1777                     {
1778                         if (this.mLocalActor.ID != actorNrToCheck && !this.mActors.ContainsKey(actorNrToCheck))
1779                         {
1780                             this.AddNewPlayer(actorNrToCheck, new PhotonPlayer(false, actorNrToCheck, string.Empty));
1781                         }
1782                     }
1783
1784                     // joinWithCreateOnDemand can turn an OpJoin into creating the room. Then actorNumber is 1 and callback: OnCreatedRoom()
1785                     if (this.mLastJoinType == JoinType.JoinOrCreateOnDemand && this.mLocalActor.ID == 1)
1786                     {
1787                         SendMonoMessage(PhotonNetworkingMessage.OnCreatedRoom);
1788                     }
1789                     SendMonoMessage(PhotonNetworkingMessage.OnJoinedRoom); //Always send OnJoinedRoom
1790
1791                 }
1792                 else
1793                 {
1794                     SendMonoMessage(PhotonNetworkingMessage.OnPhotonPlayerConnected, this.mActors[actorNr]);
1795                 }
1796                 break;
1797
1798             case EventCode.Leave:
1799                 this.HandleEventLeave(actorNr);
1800                 break;
1801
1802             case EventCode.PropertiesChanged:
1803                 int targetActorNr = (int)photonEvent[ParameterCode.TargetActorNr];
1804                 Hashtable gameProperties = null;
1805                 Hashtable actorProps = null;
1806                 if (targetActorNr == 0)
1807                 {
1808                     gameProperties = (Hashtable)photonEvent[ParameterCode.Properties];
1809                 }
1810                 else
1811                 {
1812                     actorProps = (Hashtable)photonEvent[ParameterCode.Properties];
1813                 }
1814
1815                 this.ReadoutProperties(gameProperties, actorProps, targetActorNr);
1816                 break;
1817
1818             case PunEvent.RPC:
1819                 //ts: each event now contains a single RPC. execute this
1820                 // Debug.Log("Ev RPC from: " + originatingPlayer);
1821                 this.ExecuteRPC(photonEvent[ParameterCode.Data] as Hashtable, originatingPlayer);
1822                 break;
1823
1824             case PunEvent.SendSerialize:
1825             case PunEvent.SendSerializeReliable:
1826                 Hashtable serializeData = (Hashtable)photonEvent[ParameterCode.Data];
1827                 //Debug.Log(serializeData.ToStringFull());
1828
1829                 int remoteUpdateServerTimestamp = (int)serializeData[(byte)0];
1830                 short remoteLevelPrefix = -1;
1831                 short initialDataIndex = 1;
1832                 if (serializeData.ContainsKey((byte)1))
1833                 {
1834                     remoteLevelPrefix = (short)serializeData[(byte)1];
1835                     initialDataIndex = 2;
1836                 }
1837
1838                 for (short s = initialDataIndex; s < serializeData.Count; s++)
1839                 {
1840                     this.OnSerializeRead(serializeData[s] as Hashtable, originatingPlayer, remoteUpdateServerTimestamp, remoteLevelPrefix);
1841                 }
1842                 break;
1843
1844             case PunEvent.Instantiation:
1845                 this.DoInstantiate((Hashtable)photonEvent[ParameterCode.Data], originatingPlayer, null);
1846                 break;
1847
1848             case PunEvent.CloseConnection:
1849                 // MasterClient "requests" a disconnection from us
1850                 if (originatingPlayer == null || !originatingPlayer.isMasterClient)
1851                 {
1852                     Debug.LogError("Error: Someone else(" + originatingPlayer + ") then the masterserver requests a disconnect!");
1853                 }
1854                 else
1855                 {
1856                     PhotonNetwork.LeaveRoom();
1857                 }
1858
1859                 break;
1860
1861             case PunEvent.DestroyPlayer:
1862                 Hashtable evData = (Hashtable)photonEvent[ParameterCode.Data];
1863                 int targetPlayerId = (int)evData[(byte)0];
1864                 if (targetPlayerId >= 0)
1865                 {
1866                     this.DestroyPlayerObjects(targetPlayerId, true);
1867                 }
1868                 else
1869                 {
1870                     if (this.DebugOut >= DebugLevel.INFO) Debug.Log("Ev DestroyAll! By PlayerId: " + actorNr);
1871                     this.DestroyAll(true);
1872                 }
1873                 break;
1874
1875             case PunEvent.Destroy:
1876                 evData = (Hashtable)photonEvent[ParameterCode.Data];
1877                 int instantiationId = (int)evData[(byte)0];
1878                 // Debug.Log("Ev Destroy for viewId: " + instantiationId + " sent by owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == actorNr) + " this client is owner: " + (instantiationId / PhotonNetwork.MAX_VIEW_IDS == this.mLocalActor.ID));
1879
1880
1881                 PhotonView pvToDestroy = null;
1882                 if (this.photonViewList.TryGetValue(instantiationId, out pvToDestroy))
1883                 {
1884                     this.RemoveInstantiatedGO(pvToDestroy.gameObject, true);
1885                 }
1886                 else
1887                 {
1888                     if (this.DebugOut >= DebugLevel.ERROR) Debug.LogError("Ev Destroy Failed. Could not find PhotonView with instantiationId " + instantiationId + ". Sent by actorNr: " + actorNr);
1889                 }
1890
1891                 break;
1892
1893             case PunEvent.AssignMaster:
1894                 evData = (Hashtable)photonEvent[ParameterCode.Data];
1895                 int newMaster = (int)evData[(byte)1];
1896                 this.SetMasterClient(newMaster, false);
1897                 break;
1898
1899             default:
1900                 if (photonEvent.Code < 200 && PhotonNetwork.OnEventCall != null)
1901                 {
1902                     object content = photonEvent[ParameterCode.Data];
1903                     PhotonNetwork.OnEventCall(photonEvent.Code, content, actorNr);
1904                 }
1905                 else
1906                 {
1907                     // actorNr might be null. it is fetched out of event on top of method
1908                     // Hashtable eventContent = (Hashtable) photonEvent[ParameterCode.Data];
1909                     // this.mListener.customEventAction(actorNr, eventCode, eventContent);
1910                     Debug.LogError("Error. Unhandled event: " + photonEvent);
1911                 }
1912                 break;
1913         }
1914
1915         this.externalListener.OnEvent(photonEvent);
1916     }
File name: PhotonNetwork.cs Copy
2246     public static bool CloseConnection(PhotonPlayer kickPlayer)
2247     {
2248         if (!VerifyCanUseNetwork())
2249         {
2250             return false;
2251         }
2252
2253         if (!player.isMasterClient)
2254         {
2255             Debug.LogError("CloseConnection: Only the masterclient can kick another player.");
2256             return false;
2257         }
2258
2259         if (kickPlayer == null)
2260         {
2261             Debug.LogError("CloseConnection: No such player connected!");
2262             return false;
2263         }
2264
2265         RaiseEventOptions options = new RaiseEventOptions() { TargetActors = new int[] { kickPlayer.ID } };
2266         return networkingPeer.OpRaiseEvent(PunEvent.CloseConnection, null, true, options);
2267     }
File name: frmCategoryRecord.cs Copy
51         public void GetData()
52         {
53             try
54             {
55                 con = new SqlConnection(cs.DBConn);
56                 con.Open();
57                 String sql = "SELECT * from Category order by CategoryName";
58                 cmd = new SqlCommand(sql, con);
59                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
60                 dataGridView1.Rows.Clear();
61                 while (rdr.Read() == true)
62                 {
63                     dataGridView1.Rows.Add(rdr[0],rdr[1]);
64                 }
65                 con.Close();
66             }
67             catch (Exception ex)
68             {
69                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
70             }
71         }
File name: frmLogin.cs Copy
25         private void btnOK_Click(object sender, EventArgs e)
26         {
27             if (txtUserName.Text == "")
28             {
29                 MessageBox.Show("Please enter user name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
30                 txtUserName.Focus();
31                 return;
32             }
33             if (txtPassword.Text == "")
34             {
35                 MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
36                 txtPassword.Focus();
37                 return;
38             }
39             try
40             {
41                 SqlConnection myConnection = default(SqlConnection);
42                 myConnection = new SqlConnection(cs.DBConn);
43
44                 SqlCommand myCommand = default(SqlCommand);
45
46                 myCommand = new SqlCommand("SELECT Username,password FROM Registration WHERE Username = @username AND password = @UserPassword", myConnection);
47                 SqlParameter uName = new SqlParameter("@username", SqlDbType.VarChar);
48                 SqlParameter uPassword = new SqlParameter("@UserPassword", SqlDbType.VarChar);
49                 uName.Value = txtUserName.Text;
50                 uPassword.Value = txtPassword.Text;
51                 myCommand.Parameters.Add(uName);
52                 myCommand.Parameters.Add(uPassword);
53
54                 myCommand.Connection.Open();
55
56                 SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
57
58                 if (myReader.Read() == true)
59                 {
60                     int i;
61                     ProgressBar1.Visible = true;
62                     ProgressBar1.Maximum = 5000;
63                     ProgressBar1.Minimum = 0;
64                     ProgressBar1.Value = 4;
65                     ProgressBar1.Step = 1;
66
67                     for (i = 0; i <= 5000; i++)
68                     {
69                         ProgressBar1.PerformStep();
70                     }
71                     con = new SqlConnection(cs.DBConn);
72                     con.Open();
73                     string ct = "select usertype from Registration where Username='" + txtUserName.Text + "' and Password='" + txtPassword.Text + "'";
74                     cmd = new SqlCommand(ct);
75                     cmd.Connection = con;
76                     rdr = cmd.ExecuteReader();
77                     if (rdr.Read())
78                     {
79                         txtUserType.Text = (rdr.GetString(0));
80                     }
81                     if ((rdr != null))
82                     {
83                         rdr.Close();
84                     }
85
86                     if (txtUserType.Text.Trim()== "Admin")
87                     {
88                         this.Hide();
89
90                         frm.masterEntryToolStripMenuItem.Enabled=true;
91                         frm.usersToolStripMenuItem.Enabled=true;
92                         frm.customerToolStripMenuItem1.Enabled=true;
93                         frm.suppliersToolStripMenuItem.Enabled=true;
94                         frm.productsToolStripMenuItem.Enabled=true;
95                         frm.recordsToolStripMenuItem.Enabled=true;
96                         frm.registrationToolStripMenuItem.Enabled=true;
97                         frm.databaseToolStripMenuItem.Enabled=true;
98                         frm.customerToolStripMenuItem.Enabled=true;
99                         frm.supplierToolStripMenuItem.Enabled=true;
100                         frm.productToolStripMenuItem.Enabled=true;
101                         frm.stockToolStripMenuItem.Enabled=true;
102                         frm.invoiceToolStripMenuItem.Enabled = true;
103                         frm.Show();
104                         frm.lblUser.Text = txtUserName.Text;
105                         frm.lblUserType.Text = txtUserType.Text;
106                     }
107                     if (txtUserType.Text.Trim() == "Sales Person")
108                     {
109                         frm.masterEntryToolStripMenuItem.Enabled = false;
110                         frm.usersToolStripMenuItem.Enabled = false;
111                         frm.customerToolStripMenuItem1.Enabled = true;
112                         frm.suppliersToolStripMenuItem.Enabled = false;
113                         frm.productsToolStripMenuItem.Enabled = false;
114                         frm.recordsToolStripMenuItem.Enabled = false;
115                         frm.registrationToolStripMenuItem.Enabled = false;
116                         frm.databaseToolStripMenuItem.Enabled = false;
117                         frm.customerToolStripMenuItem.Enabled = true;
118                         frm.supplierToolStripMenuItem.Enabled = false;
119                         frm.productToolStripMenuItem.Enabled = false;
120                         frm.stockToolStripMenuItem.Enabled = false;
121                         frm.invoiceToolStripMenuItem.Enabled = true;
122                         this.Hide();
123                         frm.Show();
124                         frm.lblUser.Text = txtUserName.Text;
125                         frm.lblUserType.Text = txtUserType.Text;
126                     }
127                     if (txtUserType.Text.Trim() == "Warehouse Worker")
128                     {
129                         frm.masterEntryToolStripMenuItem.Enabled = false;
130                         frm.usersToolStripMenuItem.Enabled = false;
131                         frm.customerToolStripMenuItem1.Enabled = false;
132                         frm.suppliersToolStripMenuItem.Enabled = false;
133                         frm.productsToolStripMenuItem.Enabled = false;
134                         frm.recordsToolStripMenuItem.Enabled = false;
135                         frm.registrationToolStripMenuItem.Enabled = false;
136                         frm.databaseToolStripMenuItem.Enabled = false;
137                         frm.customerToolStripMenuItem.Enabled = false;
138                         frm.supplierToolStripMenuItem.Enabled = false;
139                         frm.productToolStripMenuItem.Enabled = false;
140                         frm.stockToolStripMenuItem.Enabled = false;
141                         frm.invoiceToolStripMenuItem.Enabled = false;
142                         this.Hide();
143                         frm.Show();
144                         frm.lblUser.Text = txtUserName.Text;
145                         frm.lblUserType.Text = txtUserType.Text;
146                     }
147
148                        if (txtUserType.Text.Trim() == "Warehouse Manager")
149                     {
150                         frm.masterEntryToolStripMenuItem.Enabled = false;
151                         frm.usersToolStripMenuItem.Enabled = false;
152                         frm.customerToolStripMenuItem1.Enabled = false;
153                         frm.suppliersToolStripMenuItem.Enabled = false;
154                         frm.productsToolStripMenuItem.Enabled = false;
155                         frm.recordsToolStripMenuItem.Enabled = false;
156                         frm.registrationToolStripMenuItem.Enabled = false;
157                         frm.databaseToolStripMenuItem.Enabled = false;
158                         frm.customerToolStripMenuItem.Enabled = false;
159                         frm.supplierToolStripMenuItem.Enabled = true;
160                         frm.productToolStripMenuItem.Enabled = true;
161                         frm.stockToolStripMenuItem.Enabled = true;
162                         frm.invoiceToolStripMenuItem.Enabled = false;
163                         this.Hide();
164                         frm.Show();
165                         frm.lblUser.Text = txtUserName.Text;
166                         frm.lblUserType.Text = txtUserType.Text;
167                     }
168                        if (txtUserType.Text.Trim() == "Customer")
169                        {
170                            frmCustomerMainMenu frm1 = new frmCustomerMainMenu();
171                            this.Hide();
172                            frm1.Show();
173                            frm1.lblUser.Text = txtUserName.Text;
174                        }
175                     }
176                 else
177                 {
178                     MessageBox.Show("Login is Failed...Try again !", "Login Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
179
180                     txtUserName.Clear();
181                     txtPassword.Clear();
182                     txtUserName.Focus();
183
184                 }
185                 if (myConnection.State == ConnectionState.Open)
186                 {
187                     myConnection.Dispose();
188                 }
189
190
191
192             }
193             catch (Exception ex)
194             {
195                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
196             }
197         }
File name: frmMainMenu.cs Copy
249         public void GetData()
250         {
251             try
252             {
253                 con = new SqlConnection(cs.DBConn);
254                 con.Open();
255                 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity),sum(Price*Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID group by Product.productID,productname,Price,Features,Quantity having(Quantity>0) order by ProductName";
256                 cmd = new SqlCommand(sql, con);
257                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
258                 dataGridView1.Rows.Clear();
259                 while (rdr.Read() == true)
260                 {
261                     dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5]);
262                 }
263                 foreach (DataGridViewRow r in this.dataGridView1.Rows)
264                 {
265                     if (Convert.ToInt32(r.Cells[4].Value) < 10)
266                     {
267                         r.DefaultCellStyle.BackColor = Color.Red;
268                     }
269                 }
270                 con.Close();
271             }
272             catch (Exception ex)
273             {
274                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
275             }
276         }
File name: frmMainMenu.cs Copy
309         private void textBox1_TextChanged(object sender, EventArgs e)
310         {
311             try{
312             con = new SqlConnection(cs.DBConn);
313                 con.Open();
314                 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity),sum(Price*Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID and ProductName like '" + txtProductName.Text + "%' group by product.ProductID,productname,Price,Features,Quantity having(quantity>0) order by ProductName";
315                 cmd = new SqlCommand(sql, con);
316                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
317                 dataGridView1.Rows.Clear();
318                 while (rdr.Read() == true)
319                 {
320                     dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4], rdr[5]);
321                 }
322                 foreach (DataGridViewRow r in this.dataGridView1.Rows)
323                 {
324                 if (Convert.ToInt32(r.Cells[4].Value) < 10)
325                 {
326                     r.DefaultCellStyle.BackColor = Color.Red;
327                 }
328             }
329                 con.Close();
330             }
331             catch (Exception ex)
332             {
333                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
334             }
335         }
File name: frmOrder.cs Copy
393         private void textBox1_TextChanged(object sender, EventArgs e)
394         {
395             try
396             {
397                 con = new SqlConnection(cs.DBConn);
398                 con.Open();
399                 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID and ProductName like '" + txtProduct.Text + "%' group by product.ProductID,productname,Price,Features,Quantity having(quantity>0) order by ProductName";
400                 cmd = new SqlCommand(sql, con);
401                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
402                 dataGridView1.Rows.Clear();
403                 while (rdr.Read() == true)
404                 {
405                     dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4]);
406                 }
407                 con.Close();
408             }
409             catch (Exception ex)
410             {
411                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
412             }
413         }
File name: frmOrder.cs Copy
444         public void GetData()
445         {
446             try
447             {
448                 con = new SqlConnection(cs.DBConn);
449                 con.Open();
450                 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID group by Product.productID,productname,Price,Features,Quantity having(Quantity>0) order by ProductName";
451                 cmd = new SqlCommand(sql, con);
452                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
453                 dataGridView1.Rows.Clear();
454                 while (rdr.Read() == true)
455                 {
456                     dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3],rdr[4]);
457                 }
458                 con.Close();
459             }
460             catch (Exception ex)
461             {
462                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
463             }
464         }
File name: frmPlaceOrders.cs Copy
404         private void textBox1_TextChanged(object sender, EventArgs e)
405         {
406             try
407             {
408                 con = new SqlConnection(cs.DBConn);
409                 con.Open();
410                 String sql = "SELECT Product.ProductID,ProductName,Features,Price,sum(Quantity) from Temp_Stock,Product where Temp_Stock.ProductID=Product.ProductID and ProductName like '" + txtProduct.Text + "%' group by product.ProductID,productname,Price,Features,Quantity having(quantity>0) order by ProductName";
411                 cmd = new SqlCommand(sql, con);
412                 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
413                 dataGridView1.Rows.Clear();
414                 while (rdr.Read() == true)
415                 {
416                     dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3], rdr[4]);
417                 }
418                 con.Close();
419             }
420             catch (Exception ex)
421             {
422                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
423             }
424         }

CloseConnection 165 lượt xem

Gõ tìm kiếm nhanh...