2017-01-20 6 views
0

detailsViewコマンドを使用してデータベースにデータを挿入しようとしています。そしてそれは働かない。ここに私のコード があります。私は正常に.aspxページに挿入することができますbutiはデータとしてもgridviewの行を使用する必要があるのでいくつかの制限があり、私はaspx.csページでそれを行う場合、私はそれを達成することができます。ここでDetailsViewコードの後ろの挿入が機能していません

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) 
{ 
    string Price; 
    string Item; 
    string PetitionType; 
    string Note; 
    string UserNameGV = (GridView1.SelectedRow.Cells[3].Text); 
    string InvoiceGV = (GridView1.SelectedRow.Cells[5].Text); 
    string CreatedDateGV = (GridView1.SelectedRow.FindControl("lblLocalTime") as Label).Text; 
    SearchTB.Text = UserNameGV + " " + InvoiceGV + " " + CreatedDateGV; 
    DateTime CreatedDate = Convert.ToDateTime(CreatedDateGV); 

    for (Int32 attempt = 1; ;) 
    { 
     using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RapidVisaConnectionString"].ConnectionString)) 
     { 
      try 
      { 
       Price = ((TextBox)DetailsView1.Rows[1].FindControl("TextBox2")).Text; 
       Item = ((DropDownList)DetailsView1.Rows[1].FindControl("DropDownList2")).SelectedValue; 
       PetitionType = ((DropDownList)DetailsView1.Rows[1].FindControl("DropDownList3")).SelectedValue; 
       Note = ((TextBox)DetailsView1.Rows[1].FindControl("TextBox2")).Text; 
       con.Open(); 
       string Sql = "INSERT INTO InvoiceDetail (Price, Item, PetitionType, Note, Paid, Quantity, Invoice, UserName, CreatedDate) VALUES (@Price, @Item, @PetitionType, @Note, @Paid, @Quantity, @Invoice, @UserName, @CreatedDate)"; 
       SqlCommand cmd = new SqlCommand(Sql, con); 
       cmd.Parameters.AddWithValue("@Price", Price); 
       cmd.Parameters.AddWithValue("@Item", Item); 
       cmd.Parameters.AddWithValue("@PetitionType", PetitionType); 
       cmd.Parameters.AddWithValue("@Note", Note); 
       cmd.Parameters.AddWithValue("@Paid", 1); 
       cmd.Parameters.AddWithValue("@Quantity", 1); 
       cmd.Parameters.AddWithValue("@Invoice", InvoiceGV); 
       cmd.Parameters.AddWithValue("@UserName", UserNameGV); 
       cmd.Parameters.AddWithValue("@CreatedDate", CreatedDate); 
       cmd.ExecuteNonQuery(); 
       return; 
      } 
      catch (SqlException sqlException) 
      { 
       // Increment Trys 
       attempt++; 
       // Find Maximum Trys 
       // Override the web.config setting of 4 for retrys for this method because we are getting time-out errors. 
       Int32 maxRetryCount = Int32.Parse(ConfigurationManager.AppSettings["ConnectionRetrys"]); 
       //Int32 maxRetryCount = 5; 
       // Throw Error if we have reach the maximum number of retries 
       if (attempt == maxRetryCount) 
       { 
        ErrorLog EL = new ErrorLog(); 
        EL.WriteErrorWithSubjectNoWriteToDB("", "Error InvoiceDetail Max Retry"); 
        //going to stop throwing an error because we are getting too many 
        //throw; 
        break; 
       } 
       // Determine if we should retry or abort. 
       if (!SQLUtilities.RetryLitmus(sqlException)) 
       { 
        ErrorLog EL = new ErrorLog(); 
        EL.WriteErrorWithSubjectNoWriteToDB("Insert Failed RetryLitmus for user " + UserName + ". Sql exception number " + sqlException.Number.ToString() + ". " + sqlException.ToString(), "Error InvoiceDetail Failed Litmus"); 
        //going to stop throwing an error because we are getting too many 
        //throw; 
        break; 
       } 
       else 
        Thread.Sleep(SQLUtilities.ConnectionRetryWaitSeconds(4)); 
       //Changed from default of 5 seconds to 3 seconds 
       //Thread.Sleep(SQLUtilities.ConnectionRetryWaitSeconds(attempt)); 
      } 
     } 
    } 
} 

私が得たエラーメッセージです。

InsertCommandが指定されていない場合、データソース 'DetailsViewDS'によって挿入がサポートされていません。問題は、aspx.csのaspx.csでのみInsertCommandを追加したくないということです。

+0

はい、ここにメッセージがあります。 InsertCommandが指定されていない限り、挿入はデータソース 'DetailsViewDS'でサポートされていません。問題は、aspx.csにのみaspx.csのInsertCommandを追加したくないということです。 – Ping

+0

InsertCommandが指定されていない限り、[挿入はデータソース 'SqlDataSource1'でサポートされていません](http://stackoverflow.com/questions/7283920 /with-not-supported-by-data-source-sqldatasource1-unless-insertcommand) –

+0

@M Adeel Khalid私はすでにこれについて言及していると思いますが、aspx.pageにinsertコマンドを追加したくないです。理由を説明してください。私のコードにはいくつかの制限があるからです。 – Ping

答えて

0

私はあなたのコードをテストし、いくつかの変更を行い、動作します。私は私のデータベースのテーブルを使用した、あなたはあなたのニーズに応じてそれを変更することができます。以下で説明するように、DetailsView1_ItemInserting内のSqlDataSourceおよびC#コードを変更します。それは完全に動作します。

をSqlDataSource:私はそれはあなたの問題を解決することを願っています

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) 
    { 
     SqlDataSource sqldsInsertPassword = new SqlDataSource(); 
     sqldsInsertPassword.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
     sqldsInsertPassword.ProviderName = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName; 
     sqldsInsertPassword.InsertCommand = "INSERT INTO Image (Name) VALUES (@Name)"; 
     sqldsInsertPassword.InsertCommandType = SqlDataSourceCommandType.Text; 
     sqldsInsertPassword.InsertParameters.Add("Name", e.Values[0].ToString()); 
     sqldsInsertPassword.Insert(); 
    } 

背後にあるコードから

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Image]" InsertCommand="INSERT INTO [Image] ([Name]) VALUES (@Name)"> 
    <InsertParameters> 
     <asp:Parameter Name="Name" Type="String" /> 
    </InsertParameters> 
</asp:SqlDataSource> 

挿入。 よろしく!

関連する問題