2014-12-01 2 views
-2

gridview에서 행을 업데이트하는 코드가 있지만 행을 편집하고 업데이트 할 때 입력 문자열이 잘못되었으므로 오류가 발생합니다. 정확한 형식. 코드의 다음 줄에 -"입력 문자열의 형식이 올바르지 않습니다."오류

cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active); 

는 참조 용으로 내 코드를 참조하십시오 : - :이 아닌 내가 아는

protected void grdPostData_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    bool IsUpdated = false; 
    //getting key value, row id 
    int Id = Convert.ToInt32(grdPostData.DataKeys[e.RowIndex].Value.ToString()); 
    //get all the row field detail here by replacing id's in FindControl("").. 
    GridViewRow row = grdPostData.Rows[e.RowIndex]; 
    string PostTitle = ((TextBox)(row.Cells[0].Controls[0])).Text; 
    string Postdesc = ((TextBox)(row.Cells[1].Controls[0])).Text; 
    // string ddlPostCategory = ((DropDownList)(row.Cells[2].Controls[0])).SelectedValue; 
    string Active = ((TextBox)(row.Cells[3].Controls[0])).Text; 

    using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString)) 
    { 
     SqlCommand cmd = new SqlCommand(); 


     cmd.CommandText = "UPDATE tbl_Post SET [email protected], [email protected], [email protected] WHERE [email protected]"; 
     cmd.Parameters.Add("@Id", SqlDbType.Int).Value = Id; 
     cmd.Parameters.Add("@title", SqlDbType.NVarChar, 155).Value = PostTitle; 
     cmd.Parameters.Add("@description", SqlDbType.NVarChar, 99999999).Value = Postdesc; 
     cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active); 

     cmd.Connection = conn; 
     conn.Open(); 
     IsUpdated = cmd.ExecuteNonQuery() > 0; 
     conn.Close(); 
    } 
    if (IsUpdated) 
    { 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('page updated sucessfully');window.location ='csrposts.aspx';", true); 
     BindGrid(); 
    } 
    else 
    { 
     //Error while updating details 
     grdPostData.EditIndex = -1; 
     //bind gridview here.. 
     //GET GDATA FROM DATABASE AND BIND TO GRID VIEW 
    } 
} 

-

<asp:GridView ID="grdPostData" runat="server" Width="100%" border="1" Style="border: 1px solid #E5E5E5;" 
    CellPadding="3" AutoGenerateColumns="False" AllowPaging="true" PageSize="4" CssClass="hoverTable" 
    OnPageIndexChanging="grdPostData_PageIndexChanging" OnRowEditing="grdPostData_RowEditing" 
    OnRowDataBound="grdPostData_RowDataBound" DataKeyNames="Id" OnRowCommand="grdPostData_RowCommand" OnRowUpdating="grdPostData_RowUpdating"> 
    <AlternatingRowStyle BackColor="#CCCCCC" /> 
    <Columns> 
     <asp:BoundField DataField="title" HeaderText="Title" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td"> 
      <ControlStyle CssClass="k-grid td"></ControlStyle> 

      <ItemStyle Width="30px"></ItemStyle> 
     </asp:BoundField> 
     <asp:BoundField DataField="description" HeaderText="Description" ItemStyle-Width="30" 
      ControlStyle-CssClass="k-grid td"> 
      <ControlStyle CssClass="k-grid td"></ControlStyle> 

      <ItemStyle Width="30px"></ItemStyle> 
     </asp:BoundField> 
     <asp:TemplateField HeaderText="Post Category" ItemStyle-Width="50"> 
      <ItemTemplate> 
       <asp:DropDownList ID="ddlPostCategory" AppendDataBoundItems="true" runat="server" 
        AutoPostBack="false"> 
        <%-- <asp:ListItem Text="Select" Value="0"></asp:ListItem>--%> 
       </asp:DropDownList> 
      </ItemTemplate> 

      <ItemStyle Width="50px"></ItemStyle> 
     </asp:TemplateField> 
     <asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" ControlStyle-CssClass="k-grid td"> 
      <ControlStyle CssClass="k-grid td"></ControlStyle> 

      <ItemStyle Width="30px"></ItemStyle> 
     </asp:BoundField> 
     <asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%"> 
      <ItemTemplate> 
       <asp:ImageButton ID="btnDelete" AlternateText="Delete" CommandName="eDelete" CommandArgument='<%# Bind("Id") %>' ImageUrl="~/images/delete.png" 
        runat="server" Width="15px" Height="15px" CausesValidation="False" 
        OnClientClick="return confirm('Are you sure you want to delete this record?')" /> 
      </ItemTemplate> 
      <HeaderStyle Width="15%"></HeaderStyle> 
     </asp:TemplateField> 
     <asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" CausesValidation="false" 
      ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png" 
      UpdateImageUrl="~/images/update.png"> 
      <ControlStyle Height="20px" Width="20px"></ControlStyle> 

      <ItemStyle Width="15px"></ItemStyle> 
     </asp:CommandField> 
    </Columns> 
    <EmptyDataTemplate> 
     No Result Found 
    </EmptyDataTemplate> 
</asp:GridView> 

또한 CS 코드를 볼 질문 할 가치가 있지만 시도했지만 찾을 수 없습니다. 도와주세요. 드롭 다운리스트의 영문에 대한

코드 : -

<div class="controls"> 
    <asp:DropDownList ID="ddlActiveInactive" CssClass="selectpicker form-control-drp wd" 
     runat="server" Style="width: 100%" ValidationGroup="AddNew"> 
     <asp:ListItem Value="1" Text="Active"></asp:ListItem> 
     <asp:ListItem Value="0" Text="InActive"></asp:ListItem> 
    </asp:DropDownList> 
</div> 
+0

디버거를 사용하고 '능동적 인 '가치를 말해주십시오. –

+0

확인하겠습니다. –

+0

@TimSchmelter : 값이 'null'이됩니다. –

답변

1

는 그것은 활성 값이 드롭 다운리스트 및 비활성

입니다 :

var ddlActive = (DropDownList)row.FindControl("ddlActiveInactive"); 
int active = -1; // or whatever 
if(ddlActive.SelectedValue != "") 
    active = int.Parse(ddlActive.SelectedValue); 
// ... 
cmd.Parameters.Add("@Active", SqlDbType.Int) 
    .Value = active != -1 ? active : System.Data.SqlTypes.SqlInt32.Null; 
+0

그래서 SqlCommand 쿼리에서 ddllist를 어떻게 호출해야합니까? –

+0

@RahulSutar : 내 대답을 편집했습니다. –

+0

감사합니다, 팀, :) –

0

사용 trim() 기능은 공백 및 int로 변환을 제거합니다. 실제로 DropDownList 's의 선택된 값을 사용 FindControl 얻고 싶은 경우에

cmd.Parameters.Add("@Active", SqlDbType.Int).Value = Convert.ToInt32(Active.Trim()); 
+0

: 'string'에는 'trim'에 대한 정의가없고 'string'유형의 첫 번째 인수를 허용하는 확장 메소드 'trim'이 없습니다. 지시문 또는 어셈블리 참조?) 오류가 발생합니다. –

+0

@Rahul Sutar : 업데이트 된 답변을 확인하고 상태를 회신하십시오. –

관련 문제