I am trying to create an application. One of the task is
to insert a row into a DB. I am using db express 5 with visual studio
8. It feels like I have done everthing under the sun, but still to no avail. I know I'm missing something somewhere I just
can't get it...so I don't spend the rest of my life on this...can someone please assist me..This is a local application and not web based...although that may follow. Thanks

Here is the connection, insert part below:

private void button3_Click(object sender, EventArgs e)
        {
            { //Inserting records in the Database Data Source
                String ScreenShotConnectionString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=ScreenShot;Workstation ID=DLDw5hdc1;Integrated Security=True";
                {
                    //Getting Connection Together
                    System.Data.SqlClient.SqlConnection sqlConnection1 =
                    new System.Data.SqlClient.SqlConnection(ScreenShotConnectionString);
                     sqlConnection1.Open();
                    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();


                    cmd.Connection = sqlConnection1;
                    cmd.CommandType = System.Data.CommandType.Text;



                    // Create the InsertCommand.
                    cmd = new SqlCommand(
                       "INSERT INTO Table_1 (ScreenShotNumber, TestScriptID, StepNumber, TesterInitials, Date, DocumentNumber) " +
                       "VALUES (@ScreenShotNumber, @TestScriptID, @TesterInitials, @Date, @DocumentNumber) ", sqlConnection1);

                    // Add the parameters for the InsertCommand.
                    cmd.Parameters.Add("@ScreenShotNumber", SqlDbType.NVarChar, 50, "ScreenShotNumber");
                    cmd.Parameters.Add("@TestScriptID", SqlDbType.NVarChar, 50, "TestScriptID");
                    cmd.Parameters.Add("@StepNumber", SqlDbType.NVarChar, 50, "StepNumber");
                    cmd.Parameters.Add("@TesterInitials", SqlDbType.NVarChar, 50, "TesterInitials");
                    cmd.Parameters.Add("@Date", SqlDbType.NVarChar, 50, "Date");
                    cmd.Parameters.Add("@DocumentNumber", SqlDbType.NVarChar, 50, "DocumentNumber");



                    try
                    {
                        //open the connection to the database

                    }



                    catch (ConfigurationErrorsException ex)
                    {
                        MessageBox.Show("Error in connection ..." + ex.Message);

                    }

                    sqlConnection1.Close();
                }

            }
        }

Recommended Answers

All 8 Replies

1) Use the code tags.
2) It is something like this.

System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(ScreenShotConnectionString);

// Create the InsertCommand.
System.Data.SqlClient.SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (ScreenShotNumber, TestScriptID, StepNumber, TesterInitials, Date, DocumentNumber) "
 +"VALUES ( " + ScreenShotNumber + " , " +  TestScriptID +" , "  + TesterInitials + " , " TesterInitials + " ,  " +  DocumentNumber ") ", sqlConnection1);
try
{

//open the connection to the database
sqlConnection1.Open();

}

catch (ConfigurationErrorsException ex)
{
MessageBox.Show("Error in connection ..." + ex.Message);

}
finally
{
sqlConnection1.Close();
}
}

}
}

3) Parameters are used with stored procedures. There is no need to use here and more over you can't.

into the try clause

connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

regards

1) Use the code tags.
2) It is something like this.

System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(ScreenShotConnectionString);

// Create the InsertCommand.
System.Data.SqlClient.SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (ScreenShotNumber, TestScriptID, StepNumber, TesterInitials, Date, DocumentNumber) "
 +"VALUES ( " + ScreenShotNumber + " , " +  TestScriptID +" , "  + TesterInitials + " , " TesterInitials + " ,  " +  DocumentNumber ") ", sqlConnection1);
try
{

//open the connection to the database
sqlConnection1.Open();

}

catch (ConfigurationErrorsException ex)
{
MessageBox.Show("Error in connection ..." + ex.Message);

}
finally
{
sqlConnection1.Close();
}
}

}
}

3) Parameters are used with stored procedures. There is no need to use here and more over you can't.

I appreciate your response. However I changed things around and
used what your wrote and it says that for each ( " + ScreenShotNumber + " , that it does not exist in the current context.
A red squigly line is underneath each variable...any clues?

ScreenShotNumber is a variable you should have defined as you used it in your first post.

cmd.Parameters.Add("@ScreenShotNumber", SqlDbType.NVarChar, 50, "ScreenShotNumber");
cmd.Parameters.Add("@TestScriptID", SqlDbType.NVarChar, 50, "TestScriptID");
cmd.Parameters.Add("@StepNumber", SqlDbType.NVarChar, 50, "StepNumber");

ScreenShotNumber is the value of ScreenShotNumber(column name) that you want to insert.

I thought you said not to use parmaeters

3) Parameters are used with stored procedures. There is no need to use here and more over you can't.

A small uer interface comes up and I am clicking the submit button
hoping to send the first list box and the other 5 textboxes to the
database after the user enters data..this is not happening:
My stuff:

private void button3_Click(object sender, EventArgs e)
        {
            { //Inserting records in the Database Data Source
                String ScreenShotConnectionString = "Data Source=(local)\\SQLEXPRESS;Initial Catalog=ScreenShot;Workstation ID=DLDw5hdc1;Integrated Security=True";

                System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(ScreenShotConnectionString); // Create the InsertCommand.System.Data.SqlClient.SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (ScreenShotNumber, TestScriptID, StepNumber, TesterInitials, Date, DocumentNumber) " +"VALUES ( " + ScreenShotNumber + " , " +  TestScriptID +" , "  + TesterInitials + " , " TesterInitials + " ,  " +  DocumentNumber ") ", sqlConnection1);try{ //open the connection to the databasesqlConnection1.Open(); } catch (ConfigurationErrorsException ex){MessageBox.Show("Error in connection ..." + ex.Message); }finally{sqlConnection1.Close();}} }} System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection(ScreenShotConnectionString);



System.Data.SqlClient.SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (ScreenShotNumber, TestScriptID, StepNumber, TesterInitials, Date, DocumentNumber) "
 +"VALUES (" + ScreenShotNumber + ", " + TestScriptID +", " + TesterInitials +", "+ Date +",  "+ DocumentNumber ")", sqlConnection1);

cmd.Parameters.Add(" + ScreenShotNumber +  ( + ScreenShotNumber + "); 

     try
{


sqlConnection1.Open();

}

catch (ConfigurationErrorsException ex)
{
MessageBox.Show("Error in connection ..." + ex.Message);

}
finally
{
sqlConnection1.Close();
}

            }
        }

Yes, I did.
In

System.Data.SqlClient.SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (ScreenShotNumber, TestScriptID, StepNumber, TesterInitials, Date, DocumentNumber) "
 +"VALUES ( " + ScreenShotNumber + " , " +  TestScriptID +" , "  + TesterInitials + " , " TesterInitials + " ,  " +  DocumentNumber ") ", sqlConnection1);

The terms ScreenShotNumber ,TestScriptID ,.... are not parameters they are the variables whose value is taken to create the query string. These values are necessary.

If you are still not clear, look at a few examples on the net. :)

1) I have attached a small solution to make it clear.
2) I apologize for a small mistake on my part, I missed the single quotes in my earlier posts.

System.Data.SqlClient.SqlCommand cmd = new SqlCommand("INSERT INTO Table_1 (ScreenShotNumber, TestScriptID, StepNumber, TesterInitials, Date, DocumentNumber) "
 +"VALUES ( '" + ScreenShotNumber + "' , '" +  TestScriptID +"' , '"  + TesterInitials + "' , '" TesterInitials + "' ,  '" +  DocumentNumber "') ", sqlConnection1);

corrected

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.