Вопрос

I have prepared this Form to upload a book to the server side db with its image.

I have to read and retrieve the book on another page thus saving image path in database and also saving image in a folder "upload" ..

I tried debugging the code, problem is that the debug arrow does not even enters the button click event.

On designing section, there's just a simple form comprising of textboxes retrieving client's information on book and also a file upload controller, within same button click event.

public partial class UploadBooks : System.Web.UI.Page
{

string strcon = WebConfigurationManager.ConnectionStrings["StudentConnectionString1"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    // add session name
    //Label3.Text = Session["StudFirstName"].ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
    // image uploading
    if (FileUpload1.HasFile)
    {
        try
        {
            if (FileUpload1.PostedFile.ContentType == "image/jpeg")
            {
                if (FileUpload1.PostedFile.ContentLength < 102400)
                {
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    FileUpload1.SaveAs(Server.MapPath("~/Uploads") + filename);
                    Label2.Text = "Upload status: File uploaded!";
                }
                else
                    Label2.Text = "Upload status: The file has to be less than 100 kb!";
            }
            else
                Label2.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch (Exception ex)
        {
            Label2.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
    {

        SqlConnection con = new SqlConnection(strcon);
        SqlCommand cmd = new SqlCommand("Insert into Books (StudId,BookId,Title,Author,Description,Price,Owner,Phone_no,ImagePath) values (@sid,@bid,@t,@a,@d,@p,@o,@n,@i)", con);
        cmd.Parameters.AddWithValue("@sid", Label4.Text);
        cmd.Parameters.AddWithValue("@bid", Label1.Text);
        cmd.Parameters.AddWithValue("@t", TextBox1.Text);
        cmd.Parameters.AddWithValue("@a", TextBox2.Text);
        cmd.Parameters.AddWithValue("@d", TextBox3.Text);
        cmd.Parameters.AddWithValue("@p", TextBox6.Text);
        cmd.Parameters.AddWithValue("@o", TextBox4.Text);
        cmd.Parameters.AddWithValue("@n", TextBox5.Text);
        cmd.Parameters.AddWithValue("@i", FileUpload1.FileName);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        Response.Write("1 Book Added");
    }


}
}
Это было полезно?

Решение

Based on your clarification in the comment it is clear that you are trying to INSERT a value into the table's IDENTITY column.

Most likely it is the StudId or the BookID column.

Remove the identity column from your cmd.Parameters.AddWithValue() statement and your INSERT string and you should be good.

SQL Automatically inserts this value and increments it based on the previous values.

If your IDENTITY column is StudId then your insert string should look like:

"Insert into Books (BookId,Title,Author,Description,Price,Owner,Phone_no,ImagePath) values (@bid,@t,@a,@d,@p,@o,@n,@i)"

If your IDENTITY column is BookId then it should look like:

"Insert into Books (StudId,Title,Author,Description,Price,Owner,Phone_no,ImagePath) values (@sid,@t,@a,@d,@p,@o,@n,@i)"

Then just delete the cmd.Parameters.AddWithValue() line for the related parameter.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top