Adeel Aslam
Adeel Aslam

Reputation: 1294

int data type is not working as parameter

I have a GridView in which I have check box as Item Template, and I am updating the GridView when check box is changed. Here is my GridView code:

<Columns>
    <telerik:GridTemplateColumn>
    <ItemTemplate>
        <asp:CheckBox ID="chkcelar" runat="server" Text="Clear" OnCheckedChanged="chkclearchng" AutoPostBack="true"/>
    </ItemTemplate>
    </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn DataField="BPV_NUM" DataType="System.Int64"
            DefaultInsertValue="" HeaderText="BPV No" SortExpression="BPV_NUM" 
            UniqueName="BPV_NUM">
        </telerik:GridBoundColumn>
</Columns>

and here is the c# code through which I am updating grid view

protected void chkclearchng(object sender, EventArgs e)
    {
        OleDbConnection con = new OleDbConnection("Data Source=sml; User ID=sml; Password=sml; provider=OraOLEDB.Oracle");
        OleDbCommand cmd  = new OleDbCommand();
        CheckBox chkcelar = ((CheckBox)(sender));
        GridDataItem  row = ((GridDataItem)(chkcelar.NamingContainer));
        long bpvnum = row.Cells[1].Text;

        if (chkcelar.Checked ) {
            cmd.CommandText = @"update sml.FND_01_11@wbg set CLR_FLG=1, CLR_DTE=sysdate where bpv_num=:bpv_num and bpv_dte=:bpv_dte";
        }
        else {
            cmd.CommandText = @"update sml.FND_01_11@wbg set CLR_FLG=0, CLR_DTE=sysdate where bpv_num=:bpv_num and bpv_dte=:bpv_dte";
        }
        cmd.CommandType = CommandType.Text;
        cmd.Connection  = con;

        cmd.Parameters.Add(":bpv_num",OleDbType.BigInt).Value = bpvnum;
        cmd.Parameters.Add(":bpv_dte",OleDbType.Date).Value   = RadComboBox1.SelectedValue;

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close(); 

    }

The problem is that when I change the check box this error appears:

Input string was not in a correct format.

Can anyone tell what may be the issue and how can I resolve it?

Upvotes: 1

Views: 456

Answers (4)

Wolf
Wolf

Reputation: 2150

The issue in your code is because of the wrong type conversion from "string" to "OleDbType.Date". So replace the following code

cmd.Parameters.Add(":bpv_dte",OleDbType.Date).Value   = RadComboBox1.SelectedValue;

with the following code

cmd.Parameters.Add(New OleDb.OleDbParameter("@bpv_dte", OleDb.OleDbType.Date));
cmd.Parameters("@bpv_dte").Value = RadComboBox1.SelectedValue.ToString("d");

Also make sure to convert all types correctly to OleDbType if there are more lines used for insertion.

Upvotes: 0

watraplion
watraplion

Reputation: 287

Using Quick window check what is the vaule returns on "row.Cells[1].Text".

Upvotes: 0

qwerty
qwerty

Reputation: 2085

May be you should use

long bpvnum = long.Parse(row.Cells[1].Text);

if doesn'T work then on this line

cmd.Parameters.Add(":bpv_dte",OleDbType.Date).Value   = RadComboBox1.SelectedValue

You need parameter type OleDbType.Date but you are assigning RadComboBox1.SelectedValue

so you need to convert your

RadComboBox1.SelectedValue to OleDbType.Date

or you should simply use DateTimePicker instead of ComboBox

Upvotes: 1

Gregor Primar
Gregor Primar

Reputation: 6805

Change your code like this:

    protected void chkclearchng(object sender, EventArgs e)
    {
        using (OleDbConnection con = new OleDbConnection("Data Source=sml; User ID=sml; Password=sml; provider=OraOLEDB.Oracle"))
        {
            con.Open();
            using (OleDbCommand cmd = new OleDbCommand(null, con))
            {
                CheckBox chkcelar = ((CheckBox)(sender));
                GridDataItem row = ((GridDataItem)(chkcelar.NamingContainer));
                long bpvnum = Convert.ToInt64(row.Cells[1].Text);
                if (chkcelar.Checked)
                {
                    cmd.CommandText = @"update sml.FND_01_11@wbg set CLR_FLG=1, CLR_DTE=sysdate where bpv_num=@bpv_num and bpv_dte=@bpv_dte";
                }
                else
                {
                    cmd.CommandText = @"update sml.FND_01_11@wbg set CLR_FLG=0, CLR_DTE=sysdate where bpv_num=@bpv_num and bpv_dte=@bpv_dte";
                }
                cmd.Parameters.Add(new OleDbParameter("@bpv_num", bpvnum));
                cmd.Parameters.Add(new OleDbParameter("@bpv_dte", Convert.ToDateTime(RadComboBox1.SelectedValue)));
                cmd.ExecuteNonQuery();
            }
        }
    }

Upvotes: 1

Related Questions