Thursday, 15 January 2015

sql server - How do I insert an item to my table properly? SQL/C# -


Due to my second query error, I have trouble reviewing my review table as my message box (debugging) I keep reading (SE 777, 57547, 4) or even if I play with the query instead of (24, 57547, 4), because when I type in a movie, it will be replaced To use as an int for different tables, the movie id number is my target To put in my table (read as 24, 57547, 4).

  Private Zero InsertReview_Click (Object Sender, EventArgs e) {string filename, connectionInfo; SqlConnection DB; This.listBox1.Items.Clear (); Filename = "netflix.mdf"; ConnectionInfo = String.Format (@ "Data Source = (LocalBible) \ v11.0; Attached FillName = | DataDirectory | \ {0}; Integrated Security = True;", File Name); Db = new SqlConnection (connectionInfo); Db.Open (); SqlCommand CMD = new SQL Commands (); CMD Connection = DB; String moviename = this.textBox1.Text; Moviename = moviename.Replace ("'", "' '); Cmd.CommandText = string.Format (@" Select movie from movies where movie name =' {0} '; ", moviename); Object result = CMD. Exclass ssl (); int id = System.Convert.ToInt32 (results); this.listBox1.Items.Add (id); // to ensure that it has been correctly converted SqlCommand cmd2 = new SqlCommand () ; CMD 2. Connection = DB; Cmd2.CommandText = // second query, this issue posted here string. Format (@ "INSERT INTO Reviews (MovieID, User ID, Rating) Well ({0}); ", id, this.Texbox 1. text;); MessageBox.Show (cmd2.CommandText); // debugging SqlDataAdapter adapter = new SqlDataAdapter (cmd); dataset DS = new dataset (); Adapter.fill (ds); db.Close (); //this.listBox1.Items.Add (Free movie review included successfully!); }  

in your string. Format:

  Cmd2.CommandText = // second query, this issue posting string here. Format (@ "INSERT INTO Reviews (MovieID, User ID, Rating) Value ({0});", id, it.Texbox 1. text;);  

You only have one format item {0} , but you are passing two arguments to enter the string. And you are trying to insert 3 columns of data in the table. I'm not sure how the user ID and ratings are being submitted, but your code should look like this:

  cmd2.CommandText = // another query, the issue came posting string here. Format (@ "INSERT INTO Reviews (MovieID, UIIID, Ratings) value ({0}, {1}, '{2}');", id, user id, rating);  

While doing this dynamic SQL is a very bad way.

You should parameter the query, such as:

  cmd2.CommandText = // another query, here image posting @ "Include in review (movie id, user id, rating ) VALUES (@MovieId, @UserId, @Rating); "); Cmd2.Parameters.AddWithValue ("@ MovieID", ID); Cmd2.Parameters.AddWithValue ("UserId", userId); Cmd2.Parameters.AddWithValue ("Ratings", Ratings);  

No comments:

Post a Comment