Results 1 to 2 of 2

Thread: Warning message adding database

  1. #1
    DavisNelson is offline Senior Member
    Join Date
    Dec 2009
    Posts
    202
    Rep Power
    3

    Default Warning message adding database

    Warning message when adding data to database

    This is my code of connection to database:

    Public void connection()
    {
    connection.ConnectionString = "Data Source=localhost;Initial Catalog=base; Integrated Security=true";
    connection.Open();
    if (connection.State == ConnectionState.Open)
    {
    Interaction.MsgBox("Connection was successful", MsgBoxStyle.Exclamation, "Status");
    }
    else
    {
    Interaction.MsgBox("Connection failed", MsgBoxStyle.Critical, "Status");
    }
    connection.Close();
    }

    Whenever I try to save the data in my database but it will give the error message:

    Here is my code:

    {
    try {
    connection();
    string strRequest = "SELECT * FROM Coaches";
    DataTable dtt = default(DataTable);
    SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter(strRequest, connection);
    DataSet oDataSet = new DataSet("Coaches");
    oSqlDataAdapter.Fill(oDataSet, "Coaches");
    dtt = oDataSet.Tables("Coaches");
    oSqlDataAdapter.InsertCommand = new SqlCommand("INSERT INTO Coaches(Name,Surname,Address,Telephone) Values(@TextBoxName_E,@TextBoxSurname_E,@TextBoxAd _E,@TextBoxTel_E)", connection);
    oSqlDataAdapter.InsertCommand.Parameters.Add("@Tex tBoxSurname_E", SqlDbType.NChar, 15, "TextBoxSurname_E");
    oSqlDataAdapter.InsertCommand.Parameters.Add("@Tex tBoxTel_E", SqlDbType.Int, 100, "TextBoxTel_E");
    DataRow oDataRow = default(DataRow);
    byte[] byteArray = { };
    oDataRow = oDataSet.Tables("Coaches").NewRow;
    oDataRow("TextBoxName_E") = TextBoxName_E.Text;
    oDataRow("TextBoxAd_E") = TextBoxAd_E.Text;
    oDataRow("TextBoxTel_E") = TextBoxTel_E.Text;
    oDataSet.Tables("Coaches").Rows.Add(oDataRow);
    oSqlDataAdapter.Update(oDataSet, "Coaches");
    oDataSet.Clear();
    dtt = oDataSet.Tables("Coaches");
    Interaction.MsgBox("Article successfully registered with", MsgBoxStyle.Information, "Status");
    connection.Close();
    }
    catch {
    Interaction.MsgBox("Failed registration");
    }

  2. #2
    CarterBaker is offline Senior Member
    Join Date
    Dec 2009
    Posts
    210
    Rep Power
    3

    Default

    Your Sub Connection closes the connection right away once opening, therefore it will not work.

    On the other hand, if the Open method is successful, the connection status will without doubt open. And so the Open method fails, you have an exception, so in any case you will never pass in the else.

    You should not use the same connection object every time it does that get you problems. In its place use a local variable that you initialized with the result of a function GetConnection:

    public SqlConnection GetConnection()
    {
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = "Data Source=localhost;Initial Catalog=base;Integrated Security=true";
    connection.Open();
    return connection;
    }
    {
    try {
    Using (connection = new GetConnection())
    string strRequest = "SELECT * FROM Coaches";
    DataTable dtt = default(DataTable);
    SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter(strRequest, connection);
    DataSet oDataSet = new DataSet("Coaches");
    oSqlDataAdapter.Fill(oDataSet, "Coaches");
    dtt = oDataSet.Tables("Coaches");

    oSqlDataAdapter.InsertCommand = new SqlCommand("INSERT INTO Coaches(Name,Surname,Address,Telephone) Values(@TextBoxName_E,@TextBoxSurname_E,@TextBoxAd _E,@TextBoxTel_E)", connection);
    oSqlDataAdapter.InsertCommand.Parameters.Add("@Tex tBoxSurname_E", SqlDbType.NChar, 15, "TextBoxSurname_E");
    oSqlDataAdapter.InsertCommand.Parameters.Add("@Tex tBoxTel_E", SqlDbType.Int, 100, "TextBoxTel_E");
    DataRow oDataRow = default(DataRow);
    byte[] byteArray = { };

    oDataRow = oDataSet.Tables("Coaches").NewRow;
    oDataRow("TextBoxName_E") = TextBoxName_E.Text;
    oDataRow("TextBoxAd_E") = TextBoxAd_E.Text;
    oDataRow("TextBoxTel_E") = TextBoxTel_E.Text;
    oDataSet.Tables("Coaches").Rows.Add(oDataRow);
    oSqlDataAdapter.Update(oDataSet, "Coaches");
    oDataSet.Clear();
    dtt = oDataSet.Tables("Coaches");
    Interaction.MsgBox("Article successfully registered with", MsgBoxStyle.Information, "Status");
    connection.Close();
    }
    catch {
    Interaction.MsgBox("Failed registration");
    }

Similar Threads

  1. ActiveX warning
    By DavisNelson in forum General Internet Terms
    Replies: 1
    Last Post: 04-22-2010, 03:23 PM
  2. Active X Warning
    By PetersonClark in forum other peripherals
    Replies: 1
    Last Post: 04-22-2010, 03:09 PM
  3. Database Normalization in SQL Database
    By Millerjames in forum Programming
    Replies: 1
    Last Post: 01-23-2010, 03:17 PM
  4. Restarting of Windows Without Warning.
    By joshin in forum Windows XP
    Replies: 6
    Last Post: 04-14-2009, 04:40 AM
  5. Browser gives the message Database Error
    By romelda54 in forum General Internet Terms
    Replies: 1
    Last Post: 01-14-2009, 08:42 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48