Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + Source_Box.Text + "' AND Destination='" + Distance_Box.Text + "'";
        {
            // create new sql command to run the query through it.
            SqlCommand scmd = new SqlCommand(selQuery, conn2);

            // open connection
            conn2.Open();

            //create a sql data reader to read the query through sql command
            SqlDataReader sqldread = scmd.ExecuteReader();

            // untill sql data reader read the query
            while (sqldread.Read())
            {
                //retrieve id from the sql mapImagedata table
                int Dbid = (int)sqldread["Id"];
                //string DbId = sqldread.GetInt32("Id").ToString();

                // this will check is there any valid id or not
                if (Dbid != null)
                {
                    // used insert query to add image with particular id name to another sql data table
                    // id of the mapimagedata table will be primary key and that primary key will be the foriegn key of the new image table 
                    String QueryStr = "INSERT INTO Add_Comment(Id,User_Comments) VALUES ('" + Dbid + "',@User_Comments)";
                    scmd1 = new SqlCommand(QueryStr, conn2);
                    
                }
            }
            //dispose command for sql data reader
            sqldread.Dispose();
            // add binary data to particular datafield in the data table
            scmd1.Parameters.AddWithValue("@User_Comments", UserComment.Text);
            
            //using execution for execute all the commands according to sqlcommand 
            scmd1.ExecuteNonQuery();
            //String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";
            //SqlCommand scmd = new SqlCommand(QueryStr, conn);
            //scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;

            conn2.Close();
            //connection close



here what happens is ?

first query select the id according to source and destination

and second query insert values to another table with first table ID

but now it's insert ID only
not UserComment?
why is that................
Posted
Updated 27-Jul-13 7:57am
v3
Comments
Drazen Pupovac 27-Jul-13 13:56pm    
Did you check that "UserComment.Text" has a value before insert? Debug it, or hard-code some value to check.
If there is any error paste it, please.

if (Dbid != null) is sufficient. int is value type it can't be null.
If you receive null from database code will be broken in this line: int Dbid = (int)sqldread["Id"];. So, check if sqldread["Id"] is not null, then if pass convert it to int.

Try to use "using" statement instead of sqldread.Dispose(); and conn2.Close();, Close and Dispose will not be executed if there is some exception.

1 solution

0) Use a parameterized query
1) If ID is an int, why put it in apostrophes?
2) Use using statements
3) No need for if (Dbid != null)

4) Why not simply use an INSERT/SELECT statement?

INSERT INTO Add_Comment(Id,User_Comments)
SELECT ID,@User_Comments
FROM MapDataImage 
WHERE Source=@Source AND Destination=@Destination
 
Share this answer
 

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



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