Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i want to update Customer Information insert work very good but when i use Edit function , i must upload image again or Error Messege
empty path name is not legal and
Compiler tell me that Error Here
C#
byte[] img=null;
                            FileStream fs = new FileStream(imgLocation1, FileMode.Open, FileAccess.Read);
                                BinaryReader br = new BinaryReader(fs);
                                img = br.ReadBytes((int)fs.Length);


What I have tried:

i want to update Customer Information insert work very good but when i use Edit function , i must upload image again or Error Messege
empty path name is not legal and
Compiler tell me that Error Here
C#
byte[] img=null;
                            FileStream fs = new FileStream(imgLocation1, FileMode.Open, FileAccess.Read);
                                BinaryReader br = new BinaryReader(fs);
                                img = br.ReadBytes((int)fs.Length);

My Edit Code Is :
C#
function Image
             public static byte[] ImageToByte(Image img)
            
                    {
                        ImageConverter converter = new ImageConverter();
                        return (byte[])converter.ConvertTo(img, typeof(byte[]));
                    }
                    private void btn_Edit_Click_1(object sender, EventArgs e)
                    {

function save Gender
C#
if (radioButton_Male.Checked == true)
{
    gender = "Male";
}

else
{
    gender = "Female";
}

Edit Code
C#
if (pictureBox_ForTRAINER.Image != null)
{

    if (ID_TextBox.Text == "") return;
    string sql = "UPDATE Customers SET Customer_Name=@t1,Phone=@t2,Kind=@t3,Customer_State=@t4,First_Date=@t5,End_Date=@t6," +
        "Cost=@t7,Paid=@t8,Remain=@t9,Training_Notes=@t10,Gender=@t11,img=@t12,Date_Paid=@t13 WHERE Customer_Id='" + txt_userId.Text.Trim() + "'";
    SqlCommand cmd = new SqlCommand(sql, DXL.conn);
    cmd.Parameters.AddWithValue("@t1", this.Name_TextBox.Text);
    cmd.Parameters.AddWithValue("@t2", phone_TextBox.Text);
    cmd.Parameters.AddWithValue("@t3", kind_TextBox.Text);

    cmd.Parameters.AddWithValue("@t4", comboBox_ShowState.Text);
    cmd.Parameters.AddWithValue("@t5", Convert.ToDateTime(FirstDate_TextBox.Text));
    cmd.Parameters.AddWithValue("@t6", Convert.ToDateTime(End_Date.Text));

    cmd.Parameters.AddWithValue("@t7", Cost_TextBox.Text);
    cmd.Parameters.AddWithValue("@t8", Paid_TextBox.Text);
    cmd.Parameters.AddWithValue("@t9", remain_TextBox.Text);

    cmd.Parameters.AddWithValue("@t10", NotesAboutTRAINING_TextBox.Text);
    cmd.Parameters.AddWithValue("@t11", gender);
    cmd.Parameters.Add(new SqlParameter("@t12", ImageToByte(pictureBox_ForTRAINER.Image)));
    cmd.Parameters.AddWithValue("@t13", Convert.ToDateTime(Date_Paid.Text));

    DXL.conn.Open();
    int x = cmd.ExecuteNonQuery();
    MessageBox.Show("DONE \r\n" + x.ToString());
    DXL.conn.Close();
    this.Close();





}

if(pictureBox_ForTRAINER.Image==null)
{


  string imgLocation1 = "";
Debugger tell me that Error Here
C#
            byte[] img=null;
        FileStream fs = new FileStream(imgLocation1, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            img = br.ReadBytes((int)fs.Length);



        if (ID_TextBox.Text == "") return;
        string sql = "UPDATE Customers SET Customer_Name=@t1,Phone=@t2,Kind=@t3,Customer_State=@t4,First_Date=@t5," +
            "End_Date=@t6,Cost=@t7,Paid=@t8,Remain=@t9,Training_Notes=@t10,Gender=@t11,img=@t12,Date_Paid=@t13 WHERE Customer_Id='" +
            txt_userId.Text.Trim() + "'";
        SqlCommand cmd = new SqlCommand(sql, DXL.conn);
        cmd.Parameters.AddWithValue("@t1", this.Name_TextBox.Text);
        cmd.Parameters.AddWithValue("@t2", phone_TextBox.Text);
        cmd.Parameters.AddWithValue("@t3", kind_TextBox.Text);

        cmd.Parameters.AddWithValue("@t4", comboBox_ShowState.Text);
        cmd.Parameters.AddWithValue("@t5", Convert.ToDateTime(FirstDate_TextBox.Text));
        cmd.Parameters.AddWithValue("@t6", Convert.ToDateTime(End_Date.Text));

        cmd.Parameters.AddWithValue("@t7", Cost_TextBox.Text);
        cmd.Parameters.AddWithValue("@t8", Paid_TextBox.Text);
        cmd.Parameters.AddWithValue("@t9", remain_TextBox.Text);

        cmd.Parameters.AddWithValue("@t10", NotesAboutTRAINING_TextBox.Text);
        cmd.Parameters.AddWithValue("@t11", gender);

        cmd.Parameters.Add(new SqlParameter("@t12", img));
        cmd.Parameters.AddWithValue("@t13", Convert.ToDateTime(Date_Paid.Text));

        DXL.conn.Open();
        int x = cmd.ExecuteNonQuery();
        MessageBox.Show("DONE \r\n" + x.ToString());
        DXL.conn.Close();
        this.Close();



    }



}
my DataGrid Code
C#
public void GetIDUpdate()
{


    string strquery = "SELECT Customer_Name, Phone, Kind , Customer_State, First_Date , End_Date, Cost,Paid, Remain, " +
        "Training_Notes, Gender, img, Date_Paid FROM Customers WHERE Customer_Id=N'" + txt_userId.Text + "'";
    SqlCommand cmd = new SqlCommand(strquery, DXL.conn);
    DXL.conn.Open();
    using (SqlDataReader dr = cmd.ExecuteReader())
    {
        if (dr.Read())
        {
            Name_TextBox.Text = dr["Customer_Name"].ToString();
            phone_TextBox.Text = dr["Phone"].ToString();
            kind_TextBox.Text = dr["Kind"].ToString();
            comboBox_ShowState.Text = dr["Customer_State"].ToString();
            FirstDate_TextBox.Text = dr["First_Date"].ToString();
            End_Date.Text = dr["End_Date"].ToString();
            Cost_TextBox.Text = dr["Cost"].ToString();
            Paid_TextBox.Text = dr["Paid"].ToString();
            remain_TextBox.Text = dr["Remain"].ToString();
            NotesAboutTRAINING_TextBox.Text = dr["Training_Notes"].ToString();
            gender = dr["Gender"].ToString();

            Date_Paid.Text = dr["Date_Paid"].ToString();



            if (dr["img"] != System.DBNull.Value)
            {
                byte[] ximg = (byte[])dr["img"];
                MemoryStream mms = new MemoryStream(ximg);
                pictureBox_ForTRAINER.Image = Image.FromStream(mms);
            }
            else
            {
                pictureBox_ForTRAINER.Image = null;
            }


        }
        else if (txt_userId.Text == "")
        {
            pictureBox_ForTRAINER.Image = null;

        }

    }
    DXL.conn.Close();

}


i want to update Customer Information insert work very good but when i use Edit function , i must upload image again or Error Messege
empty path name is not legal and
Compiler tell me that Error Here
C#
    byte[] img=null;
FileStream fs = new FileStream(imgLocation1, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    img = br.ReadBytes((int)fs.Length);
Posted
Updated 8-Jul-22 18:28pm
v2

1 solution

First off, why are you passing the UserID / CunstomerID by string concatenation when all your other info is passed as a parameter? You must know that that is very dangerous, or you wouldn't have used parmateres for all the other values you are passing, but I'll reiterate it for you: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Now the problem you have noticed: "empty path name is not legal" is not a compiler error, it's a runtime error: you get it while your code is executing and teh compiler finished it's job long before that happens.

What's happing is that this line of code:
FileStream fs = new FileStream(imgLocation1, FileMode.Open, FileAccess.Read);
is trying to open an existing file, but the variable imgLocation1 contains an empty string. Somewhere in your code is the equivalent of this:
string imgLocation1 = "";
And you do not change the value after that.
And you can't open a file unless you tell the system exactly where the file is and what it is called!

Start by proving it: use the debugger and put a breakpoint on the FileStream line and when it hits, look at imgLocation1 to see exactly what it contains.

Then go back to the editor, and find all references to imgLocation1 and put a breakpoint on each of those.
Now when you run the debugger, you will find out which modifications are being executed, and which aren't.
Then it's going to be up to you to work out why there isn't a valid path in the variable!
 
Share this answer
 
Comments
ahmedbelal 9-Jul-22 0:56am    
Thank you for your Helping me OriginalGriff
but i want tell him to save image null if it's null during Edit Custmer data without upload image again
but it force me to upload image again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900