1
votes

The error I had before was I can't insert NULL value into resources_id so I added resources_id into insert value to avoid this issue, but when I do it I get this error:

ORA-01722: invalid number C#

Line 56: cmd.ExecuteNonQuery();.

WHat I did before to avoid this issue is I removed not Null and left everything null in my table and it works but it caused me other troubles which I found hard to solve, so I decided to go back to main issue and added NOT NULL to the resources_id in my database. Please help solve this issue.

Another issue I have is resources_id in the parameters is not recognised

MY CODE:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Configuration;
using System.IO;



public partial class Lecturer_upload_resources : System.Web.UI.Page
{
    string strCon = "Data Source=****;Persist Security Info=True;User ID=****;Password=****;Unicode=false";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridviewData();
        }
    }
    // Bind Gridview Data
    private void BindGridviewData()
    {
        using (OracleConnection con = new OracleConnection(strCon))
        {
            using (OracleCommand cmd = new OracleCommand())
            {
                cmd.CommandText = "select * from resource1";
                cmd.Connection = con;
                con.Open();
                gvDetails.DataSource = cmd.ExecuteReader();
                gvDetails.DataBind();
                con.Close();
            }
        }
    }
    // Save files to Folder and files path in database
    protected void btnUpload_Click(object sender, EventArgs e)
    {
          string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
        Stream str = fileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(str);
        Byte[] size = br.ReadBytes((int)str.Length);
        using (OracleConnection con = new OracleConnection(strCon))
        {
            using (OracleCommand cmd = new OracleCommand())
            {
                cmd.CommandText = "insert into resource1(Resources_id,FileName,fileType,Filedata) values(:Resources_id,:FileName,:FileType,:FileData)";
                cmd.Parameters.AddWithValue(":Resources_id",Resources_id);
                cmd.Parameters.AddWithValue(":FileName", filename);
                cmd.Parameters.AddWithValue(":FileType", "application/word");
                cmd.Parameters.AddWithValue(":FileData", size);
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                BindGridviewData();
            }
        }
    }
1
I don't see you declare or assign any value to Resources_id anywhere? - jpw
As I said I added it resources_id to sort out the issue I had before,so I dont know what to add more for my code to work.Ii just added to the parameters as you can see in my code - user2884405
The other parameters are declared and assigned values (like name), you need to do the same for Resources_id. You have to decide what value should be inserted for it and what type is appropriate.. - jpw
Thx for the replay.When I try to define it like string Resources_id = FileUpload1.PostedFile.Resources_id; I get error for the PostedFile is not recognised - user2884405
Maybe what you want is to have the Resources_id field be auto-incremented on the server side? If so you could set the AUTO_INCREMENT attribute on that column and leave it out of the insert all together. - jpw

1 Answers

1
votes

Try to use this

cmd.BindByName = true;

in insert block Or try to delete ":"