2012-03-10 9 views
0
I have a gridview which is being populated at runtime(using sessions) by entering the respective values in the web form. 
Now i want to retrieve all the data from that gridview and fill it into my table(MySql table).. 

How can i do that??? 

// GridViewのGridViewのGridViewのからデータを取得し、mysqlのに埋める

protected void btn_InsertSim_Click(object sender, EventArgs e) 
    { 
     reporting tableValues = new reporting(); 

     int t = 0; 
     int price = 0; 

     try 
     { 

      #region Insert values in gridview 
      connection.Open(); 
      string devicetyp = null; 
      string mobile = null; 

      try 
      { 


       string mobiledata="select MobileNumber from siminventoryin where SimImei='"+txt_SubjectIMEI.Text+"'"; 
       MySqlCommand cmd5=new MySqlCommand (mobiledata,connection); 
       cmd5.ExecuteNonQuery(); 
       MySqlDataReader rdr=cmd5.ExecuteReader(); 
       while (rdr.Read()) 
       { 
        mobile = (string)rdr["MobileNumber"]; 
       } 
       rdr.Close(); 


       string devicedata = "select Devicetype from deviceinventoryin where     DeviceIMEI='" + ddl_adjoiningdeviceSerial.SelectedValue.ToString() + "'"; 
       MySqlCommand cmd6 = new MySqlCommand(devicedata, connection); 
       cmd6.ExecuteNonQuery(); 
       MySqlDataReader rd = cmd6.ExecuteReader(); 
       while (rd.Read()) 
       { 
        devicetyp = (string)rd["DeviceType"]; 
       } 
       rd.Close(); 
       AddDataToTable2(txt_SubjectIMEI.Text, mobile, txt_SubjectID.Text, ddl_accountid.SelectedValue, devicetyp, ddl_adjoiningdeviceSerial.SelectedValue, txt_issuedby.Text, txt_invoiceno.Text, txt_invoicedate.Text, txt_issuedto.Text, txt_name.Text, txt_compname.Text, txt_address.Text, (DataTable)Session["myDatatable2"]); 


       grd_showsim.DataSource = ((DataTable)Session["myDatatable2"]).DefaultView; 
       grd_showsim.DataBind(); 



      } 
      catch (Exception ex) 
      { 
       ShowAlertMessage(ex.Message.ToString()); 
      } 
      finally 
      { 

      } 
      #endregion 
     } 
     catch 
     { 
      ShowAlertMessage("Oops, Some error occured... We are working on it"); 
     } 
     finally 
     { 
      connection.Close(); 
      //txt_SubjectID.Text = ""; 
      //txt_SubjectIMEI.Text = ""; 

     } 
    } 
    #endregion 

//データバインドさにeneteredされている値をクリックして上のデータテーブル

private DataTable CreateDataTable2() 
    { 
     DataTable myDataTable2 = new DataTable(); 
     DataColumn myDataColumn; 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Sim Imei"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Mobile Number"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Sim ID"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "IssuedForAccount"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "IssuedForDevice"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Device Imei"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Issued By"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Invoice Number"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Invoice Date"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Issued To"; 
     myDataTable2.Columns.Add(myDataColumn); 

     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Authority Name"; 
     myDataTable2.Columns.Add(myDataColumn); 


     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Company Name"; 
     myDataTable2.Columns.Add(myDataColumn); 


     myDataColumn = new DataColumn(); 
     myDataColumn.DataType = Type.GetType("System.String"); 
     myDataColumn.ColumnName = "Company Address"; 
     myDataTable2.Columns.Add(myDataColumn); 




     return myDataTable2; 
    } 
//creating function to add data 
private void AddDataToTable2(string SimImei, string MobileNumber, string SimID, string IssuedForAccount, string IssuedForDevice, string DeviceImei, string IssuedBy, string InvoiceNumber, string InvoiceDate, string IssuedTo, string AuthorityName, string CompanyName, string Address, DataTable myTable) 
    { 
     DataRow row = myTable.NewRow(); 
     row["Sim Imei"] = SimImei; 
     row["Mobile Number"] = MobileNumber; 
     row["Sim ID"] = SimID; 
     row["IssuedForAccount"] = IssuedForAccount; 
     row["IssuedForDevice"] = IssuedForDevice; 
     row["Device Imei"] = DeviceImei; 
     row["Issued By"] = IssuedBy; 
     row["Invoice Number"] = InvoiceNumber; 
     row["Invoice Date"] = InvoiceDate; 
     row["Issued To"] = IssuedTo; 
     row["Authority Name"] = AuthorityName; 
     row["Company Name"] = CompanyName; 
     row["Company Address"] = Address; 
     myTable.Rows.Add(row); 
    } 

public static void ShowAlertMessage(string error) 
    { 
     Page page = HttpContext.Current.Handler as Page; 
     if (page != null) 
     { 
      error = error.Replace("'", "\'"); 
      ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true); 
     } 
    } 

//ボタンを作成します

protected void grd_showsim_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
Label lblSerial = (Label)e.Row.FindControl("lblSerial"); 
lblSerial.Text = i.ToString(); 
i++; 
} 
} 

助けてください... !!

答えて

0

gridviewコントロール&を参照してforeachを実行するには、個々のテンプレートコントロールまたはEvalを使用します。何かのように

try{ 
foreach (GridViewRow gr in YourGridView.Rows) 
{ 
    TextBox tb1 = ((TextBox) gr.FindControl("YourControlId1")).Text; 
    TextBox tb2 = ((TextBox) gr.FindControl("YourControlId2")).Text; 
    // Insert to Database here 
    // Open Connection ; ExecuteNonQuery(); 
    } 
} 
catch(Exception ex) 
{ 
    // log exception 
} 
+0

あなたの助けを感謝Damien ..!あなたのコードは魔法のように働いて..!助けてくれてありがとう!!! – writeToBhuwan

+0

@Bhuwan Devshali。私はあなたのために働いてうれしいです。あなたが私の答えを気に入ったら、あなたはそれをアップホート&答えとしてマークすることができます。ありがとうございました。ダミアン。 –

+0

@Bhawanねえ、あなたのために魔法のように働いたのはどうして私の答えを受け入れなかったのですか?代わりにあなたは新しい投稿を作成し、それを答えとしてマークしました:(Damien。 –

関連する問題