locked
New to VB RRS feed

  • Question

  • Not sure if there is an easy answer but ill see if i can word the question correctly and hopefully i will get the answer i need.

    I am designing a simple application (1 window) to write data to a RSS feed ... stored in a MySQL database.

    The page consists of 3 boxes

    Title
    Content
    Date

    ID is generated automaticly by the SQL server upon insert.

    I have a submit button on the page...

    currently the code i have is

    Code Snippet

            private void button1_Click(object sender, EventArgs e)
            {
             
            }


    This is as far as my knowledge goes...

    I wish to simply post (php programmer sorry) the 3 boxes to the SQL server for saving, I have created a DSN to connect to the database but I have no idea how useful that actually is as i have no clue how to write the data to the database.

    I would assume that when I click the submit button the code within the { }'s is executed to save the data to the database... what do I put in the { }'s to make this all work... once this works I will prob arrange to start making a sql viewer/editor and increate its editing potential but to start with i just want it saving to the database....

    Nice (simple) one for you.

    Thanks

    Keith
    Tuesday, August 21, 2007 11:20 PM

All replies

  • Firstly, the language is C# - not VB.NET :-)

     

    If it were in VB.NET, your code would look something like the following:

     

     

    private sub button1_Click(byval sender as Object, byval e as EventArgs) Handles button1.Click

     

     

    end sub

     

    Now...for the remainder of the question

     

    you need:

     

    1) Establish a connection to SQL Server

    2) Generate your INSERT statement

    3) Execute the non query.

     

    The Button Click event, as you posted, will be the one where this code will execute. It's best practice to use Stored Procedures to insert data but for the sake of simplicity for you, we will do the normal code insertion. Take a look at the following:

    Code Snippet

     

    string connectionString = "Data Source=(ServerName);Initial Catalog=DataBaseName;Trusted_Connection=true;";

    SqlConnection connection = new SqlConnection(connectionString);

    SqlCommand command = new SqlCommand("INSERT INTO [TableName] (Field1, Field2, Field3) VALUES (@p1, @p2, @p3)", connection)

     

    //create the parameters:

     

    SqlParameter p1 = new SqlParameter("@p1", ValueHere);

    SqlParameter p2 = new SqlParameter("@p2", ValueHere);

    SqlParameter p3 = new SqlParameter("@p3", ValueHere);

     

    //add the parameters to the command parameter collection so it has values to look up to and insert them into the fields

     

    command.Parameters.Add(p1);

    command.Parameters.Add(p2);

    command.Parameters.Add(p3);

     

    //now insert data!

     

    try

    {

       command.Connection.Open(); //open the connection

       command.ExecuteNonQuery(); //execute the command

       command.Connection.Close(); //close the connection

    }

    catch (SqlException ex)

    {

       //handle error

    }

     

     

     

     

    And thats all there is to it. Of course, you need to modify some variables to your needs, including your INSERT statement.

     

    Does this help? Do you require further information?

    Tuesday, August 21, 2007 11:46 PM