1
    <asp:DropDownList ID="DDLStatusChange" runat="server" AppendDataBoundItems="true">
    <asp:ListItem>-Select-</asp:ListItem>
    <asp:ListItem>Just Received</asp:ListItem>
    <asp:ListItem>Post Approval</asp:ListItem>
    </asp:DropDownList>
    <asp:TextBox ID="txtStatusChange" runat="server" Width="200"></asp:TextBox>
    <asp:Button ID="btnStatuschange"
        runat="server" Text="Status Change" onclick="btnStatuschange_Click" />

Code Behind C#

protected void btnStatuschange_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OptimaWebCustomerQueryCon"].ConnectionString))
        {
            conn.Open();
            string str = @"Update TblMasterinfo SET AppLoanStatus=@AppLoanStatus Where appid in (@Statuschange)";
            using (SqlCommand com = new SqlCommand(str, conn))
            {
                com.Parameters.AddWithValue("@Statuschange", Convert.ToInt32(txtStatusChange.Text));
                com.Parameters.AddWithValue("@AppLoanStatus",DDLStatusChange.SelectedValue);
                com.ExecuteNonQuery();
            }

            conn.Close();
        }
    }

Problem:

I input into txtStatusChange these appid for update record: 17110,17147,17524,17736,15906,17268,16440,17241 mention that when I update single recored like 17110, that update successfully. error is occured when more than one recored are updated

Exception Details: System.FormatException: Input string was not in a correct format.

Sabilv
  • 602
  • 1
  • 15
  • 44
Nur Muhammad
  • 121
  • 1
  • 5
  • 13

2 Answers2

2

Try creating a List from your input

var ids = (from x in String.Split(',', txtStatusChange.Text)
           select int.Parse(x.Trim())).ToList();

com.Parameters.AddWithValue("@Statuschange", ids);

Update: To avoid using a List parameter (which might not work for sql sever) you could use this approach:

var ids = (from x in String.Split(',', txtStatusChange.Text)
           select int.Parse(x.Trim())).ToList();    
var idString = String.Join(',', ids);

var str = String.Format(
    @"Update TblMasterinfo SET ... Where appid IN ({0})", idString);

Since you are casting your string into a List and creating a new String this is save from SQL injection so I woudn't mind not using a parameter here.

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
0

You can't use normal SQL parameters for IN clause but you can use table valued parameters. check this answer. but it is bit complex

You can create sql string without parameter as below. That will be easy way but make sure that your input properly validated

string input ="17110,17147,17524,17736,15906,17268,16440,17241";
string str = string.Format("Update TblMasterinfo SET AppLoanStatus=@AppLoanStatus Where appid in ({0})",input);
using (SqlCommand com = new SqlCommand(str, conn))
{
    com.Parameters.AddWithValue("@Statuschange", Convert.ToInt32(txtStatusChange.Text));
    com.ExecuteNonQuery();
}
Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153