home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_InteropADOCS_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-03-11  |  7.4 KB  |  244 lines

  1. // This C# file is imported by ADOCS.aspx page 
  2.  
  3.  
  4.         private ArrayList PublisherID;  // need an arraylist for a drowpdownlist filled with publisher ID's when updating a record  
  5.         private System.Data.OleDb.OleDbDataAdapter myadapter;
  6.         private DataSet mydataset;
  7.          private ADODB.Connection cn;
  8.         private ADODB.Recordset rs;
  9.         private OleDbCommand myCommand;
  10.         private OleDbConnection myConnection; 
  11.         
  12. protected void Page_Load(Object Src, EventArgs E)
  13.       
  14. {
  15.     
  16.         Message1.InnerHtml = "";
  17.         
  18.         String strConn = "Provider=SQLOLEDB.1;Data Source=(local);Integrated Security=SSPI;Initial Catalog=pubs;";
  19.         cn = new ADODB.Connection();
  20.         cn.ConnectionString=strConn;
  21.         try
  22.         {
  23.             cn.Open(strConn,"","",-1);  // change your userId and password if any
  24.                 
  25.         }
  26.         catch (Exception e)
  27.             {
  28.             Response.Write(e.ToString());
  29.             Response.Flush();
  30.             Response.End();
  31.             }
  32.         
  33.     
  34.         rs= new ADODB.Recordset();
  35.         // open a recordset on a table titles with cursor LockType adOpenOptimic
  36.         // on wich the provider locks the records when updated only
  37.         // with CursorType adOpenKeyset to allow batch updates-->
  38.     
  39.         rs.Open("select * from titles",cn,ADODB.CursorTypeEnum.adOpenKeyset,ADODB.LockTypeEnum.adLockOptimistic, (int)ADODB.CommandTypeEnum.adCmdText);
  40.     
  41.         
  42.         if (rs.BOF || rs.EOF) 
  43.         {
  44.          Response.Write("No records found. Please check the SQL table"); 
  45.         }
  46.  
  47.          
  48.         //Initialize the data adapter and fill in with data from the recordset
  49.  
  50.         myadapter = new System.Data.OleDb.OleDbDataAdapter();
  51.  
  52.         // using managed connection and command objects at the same time to fill in an ArrayList for the managed DropDownList control
  53.   
  54.         
  55.         mydataset = new DataSet();
  56.         
  57.         myadapter.Fill(mydataset,rs,"titles");
  58.             
  59.         // set the source for the managed control datagrid
  60.         MyDataGrid.DataSource=mydataset;
  61.         if (!IsPostBack)
  62.         { 
  63.             MyDataGrid.DataBind();
  64.         }
  65.         cn.Close();
  66.  
  67.         string mySelectPublisher = "select pub_id from publishers";
  68.         
  69.         myConnection = new OleDbConnection(strConn);
  70.  
  71.         myCommand = new OleDbCommand(mySelectPublisher,myConnection);
  72.         myConnection.Open();
  73.         OleDbDataReader myReader;
  74.         myReader = myCommand.ExecuteReader();
  75.         PublisherID = new ArrayList();
  76.         while (myReader.Read())
  77.             { PublisherID.Add(myReader["pub_id"]);}
  78.     
  79.         myConnection.Close();
  80.  
  81.     }
  82.  
  83.     private int GetPubIndex(String pub) // this function is called when the selected item changes in the dropdownlist 
  84.         {
  85.             if (PublisherID.IndexOf(pub) != -1)
  86.                     return PublisherID.IndexOf(pub);
  87.             else
  88.                     return 0;
  89.         }
  90.  
  91.  
  92.     private void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e)
  93.     {
  94.                 MyDataGrid.EditItemIndex = -1;
  95.                 DataBind();
  96.     }
  97.     
  98.     private void MyDataGrid_Update(object sender, DataGridCommandEventArgs E)
  99.  
  100.     {
  101.         // getting the value from the TextBoxes   
  102.         String[] cols = {"title","type","price","advance","royalty","ytd_sales","notes","pubdate"};
  103.         String[] colvalue=new String[8];  
  104.         //to change the values in the dataset:
  105.                for (int i=0; i<8; i++)
  106.             {
  107.                    colvalue[i] = ((TextBox)E.Item.FindControl("edit_" + cols[i])).Text.Trim();
  108.             if (colvalue[i].Equals(String.Empty) && cols[i].Equals("title"))
  109.                 { Response.Write(" The title field can not be empty. Please insert a title");
  110.                     goto label;
  111.                 }
  112.             if (colvalue[i].Equals(String.Empty) && cols[i].Equals("type"))
  113.                     colvalue[i] = "UNDECIDED";
  114.             if ((colvalue[i].Equals(String.Empty)) && (cols[i].Equals("pubdate")))
  115.                     colvalue[i] = System.DateTime.Now.ToString(); 
  116.             if (colvalue[i].Equals(String.Empty)&& cols[i].Equals("notes"))    
  117.                     colvalue[i]="";
  118.             else if (colvalue[i].Equals(String.Empty))
  119.                     colvalue[i]=Convert.ToString(0);
  120.             try
  121.             {
  122.                  mydataset.Tables["titles"].Rows[(int)E.Item.ItemIndex][cols[i]]= colvalue[i];
  123.             }     
  124.             catch(Exception e)
  125.             {     Response.Write("\nNumeric value or date expected! Try again\n");
  126.                         goto label;    
  127.             }
  128.         }
  129.         mydataset.Tables["titles"].Rows[(int)E.Item.ItemIndex]["pub_id"]=
  130.         (((DropDownList)E.Item.FindControl("edit_pubID")).SelectedItem.ToString());
  131.         // an SQL statement is needed to call update command on the adapter object
  132.         
  133.         String updateCmd= "update titles set ";
  134.         updateCmd+="title=" + RepSQ(colvalue[0]);
  135.         updateCmd+=", type=" + RepSQ(colvalue[1]);
  136.         updateCmd+=", pub_id='" + (((DropDownList)E.Item.FindControl("edit_pubID")).SelectedItem.ToString()) + "'";
  137.         try 
  138.         {
  139.             updateCmd+=", price=" + Convert.ToDouble(colvalue[2]);
  140.             updateCmd+= ",advance=" + Convert.ToDouble(colvalue[3]);
  141.             updateCmd+= ",royalty="+ RepSQ(colvalue[4]);
  142.             updateCmd+= ",ytd_sales="+ (colvalue[5]);
  143.         }
  144.  
  145.         catch (Exception e)
  146.             {    
  147.                 Response.Write("\nNumeric value expected! Try again\n");
  148.                 goto label;    
  149.                         
  150.             }
  151.                     
  152.         updateCmd+=",notes="+ RepSQ(colvalue[6]);
  153.         updateCmd+=", pubdate="+ RepSQ(colvalue[7]);
  154.         
  155.         updateCmd+=" where title_id like " + "'" + (MyDataGrid.DataKeys[(int)E.Item.ItemIndex].ToString()).Trim() + "'" ;  
  156.         OleDbCommand myManagedCommand = new OleDbCommand();
  157.         myManagedCommand.CommandText = updateCmd;
  158.         myManagedCommand.Connection= myConnection;
  159.         myConnection.Open(); 
  160.         myadapter.UpdateCommand=myManagedCommand;
  161.                 
  162.         try
  163.         {
  164.  
  165.             myadapter.Update(mydataset,"titles"); 
  166.         }
  167.  
  168.         catch(OleDbException e)
  169.         {        Message1.InnerHtml = "ERROR: Could not update record, please ensure the fields are correctly filled out:\n";
  170.                 goto label;
  171.                 }
  172.         myConnection.Close();
  173.         label:    
  174.         MyDataGrid.EditItemIndex = - 1;
  175.            DataBind();
  176.  
  177.     }
  178.     private void MyDataGrid_Edit(object sender,DataGridCommandEventArgs E)
  179.     {
  180.  
  181.         MyDataGrid.EditItemIndex = (int)E.Item.ItemIndex;
  182.         DataBind();
  183.     }
  184.     
  185.     private void MyDataGrid_Delete(object sender, DataGridCommandEventArgs E)
  186.  
  187.     {
  188.         Message1.InnerHtml="Deleted the selected record";
  189.         OleDbCommand myManagedCommand = new OleDbCommand();
  190.         
  191.         
  192.         myManagedCommand.Connection= myConnection;
  193.         
  194.         myManagedCommand.CommandText = "Delete from titleauthor where title_id = '" +
  195.                         MyDataGrid.DataKeys[(int)E.Item.ItemIndex] + "'";
  196.         myConnection.Open();
  197.         myManagedCommand.ExecuteNonQuery();
  198.         
  199.         myManagedCommand.CommandText= "Delete from sales where title_id = '" +
  200.                         MyDataGrid.DataKeys[(int)E.Item.ItemIndex] + "'";
  201.  
  202.         myManagedCommand.ExecuteNonQuery();
  203.         myManagedCommand.CommandText = "Delete from roysched where title_id = '" +
  204.                         MyDataGrid.DataKeys[(int)E.Item.ItemIndex] + "'";     
  205.         myManagedCommand.ExecuteNonQuery();
  206.  
  207.  
  208.         // delete the row in the dataset where the pub_id is 
  209.         mydataset.Tables["titles"].Rows[(int)E.Item.ItemIndex].Delete();
  210.         // update the database table "titles"
  211.         myManagedCommand.CommandText="Delete from titles where title_id = '" +
  212.                         MyDataGrid.DataKeys[(int)E.Item.ItemIndex] + "'";         
  213.         myadapter.DeleteCommand=myManagedCommand;
  214.         
  215.         try
  216.         {
  217.                         
  218.             myadapter.Update(mydataset,"titles"); 
  219.         }
  220.  
  221.         catch(OleDbException e)
  222.         {        Message1.InnerHtml = "ERROR: Could not delete record,Try again:" + myManagedCommand;
  223.                 Message1.Style["color"] = "red";
  224.         }
  225.         
  226.         
  227.         myConnection.Close();    
  228.         MyDataGrid.EditItemIndex = - 1;
  229.            DataBind();
  230.         
  231.     }
  232.     // use this method to replace the inserted string with 'string' for the SQL statement 
  233.     private String RepSQ( String strInp )
  234.     {
  235.         String sOut = "null";
  236.  
  237.         if ( strInp.Length == 0 )
  238.             return sOut;
  239.         else    
  240.             return "'" + Regex.Replace( strInp, "'", "''" ) + "'";
  241.     }
  242.  
  243.  
  244.