9 Replies - 1551 Views - Last Post: 16 April 2012 - 08:37 AM Rate Topic: -----

#1 samban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-April 12

can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 06:44 AM

please help me. I developed an windows application using visual basic.NET. I configured my database connection string
in app.config file. The problem is that I can retrieve data but cannot insert data into the database. the config file
looks like this

  <configSections>

    </configSections>
    <connectionStrings>
        <add name="POS.My.MySettings.POS1ConnectionString" connectionString="Data Source=|DataDirectory|\Db\POS1.sdf"
            providerName="Microsoft.SqlServerCe.Client.3.5" />
        <add name="POS.My.MySettings.POS2ConnectionString" connectionString="Data Source=|DataDirectory|\Db\POS2.sdf"
            providerName="Microsoft.SqlServerCe.Client.3.5" />
    </connectionStrings>


PLEASE HELP ME

This post has been edited by Atli: 16 April 2012 - 06:45 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: can't insert data into sql through app.config in vb.net

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 06:55 AM

Are you receiving errors? If so what, and where? What does your insert SQL statement look like? Heck - let's see your code surrounding your insert statement!
Was This Post Helpful? 0
  • +
  • -

#3 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 07:01 AM

ussually, if you can retrive data from database it mean you can insert too, have you ever double check your code to insert?
Was This Post Helpful? 0
  • +
  • -

#4 samban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-April 12

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 07:06 AM

MY CONNECTION CODE LOOKS LIKE THIS
Imports System.IO
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration.ConfigurationManager

Public Class The_admin
    Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("CONN").ConnectionString)


MY INSERT CODE LOOKS LIKE THIS
 Private Sub btncreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncreate.Click

        'check if record exists
        Dim Comm As SqlCommand = sqlCon.CreateCommand()
        Comm.Connection = sqlCon
        sqlCon.Open()
        Dim Trans As Data.SqlClient.SqlTransaction = sqlCon.BeginTransaction
        Comm.Transaction = Trans

        Comm.CommandText = "Insert into LOGIN (STAFF_NAME,USERNAME,PASSWORD,ACCESS_TYPE) VALUES (@STAFF_NAME,@USERNAME,@PASSWORD,@ACCESS_TYPE)"

        ' Add Parameters to Command Parameters collection
        Comm.Parameters.Add("@STAFF_NAME", SqlDbType.NVarChar, 150)
        Comm.Parameters.Add("@USERNAME", SqlDbType.NVarChar, 50)
        Comm.Parameters.Add("@PASSWORD", SqlDbType.NVarChar, 50)
        Comm.Parameters.Add("@ACCESS_TYPE", SqlDbType.NVarChar, 10)

        Comm.Parameters("@STAFF_NAME").Value = txtStaffName.Text.Trim
        Comm.Parameters("@USERNAME").Value = txtuser.Text.Trim
        Comm.Parameters("@PASSWORD").Value = txtpass.Text.Trim
        Comm.Parameters("@ACCESS_TYPE").Value = CboUSERTYPE.Text.Trim
        Comm.ExecuteNonQuery()
        Comm.Parameters.Clear()

        'Commit(Process)
        Try
            Trans.Commit()
            MessageBox.Show("Account successfully created", "Account created", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show("Account not successfully created", "Account creation failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Trans.Rollback()
        End Try
        sqlCon.Close()

    End Sub

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 07:13 AM

Are you picking up an exception?

Side note - it's best to put your 'executenonquery' INSIDE the try/catch... you know so if there is a problem the try/catch can do something about it.
Was This Post Helpful? 0
  • +
  • -

#6 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 07:21 AM

and what is your error message? is there any condition that make user failed to make account? and what condition make it happened?

aggree with modi123_1, not a good idea if you use exception like that.

This post has been edited by artemix22: 16 April 2012 - 07:23 AM

Was This Post Helpful? 0
  • +
  • -

#7 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 448
  • View blog
  • Posts: 3,103
  • Joined: 12-January 10

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 07:24 AM

out of curioousity did you make sure your table is not set to read only?
Was This Post Helpful? 0
  • +
  • -

#8 samban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-April 12

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 07:25 AM

THANKS
BUT I AM NOT PICKING ANY ERROR. I HAVE PUT THE EXECUTENONQUERY IN THE TRY CATCH AND I COULD NOT INSERT DATA
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 08:13 AM

Okay so let's break down the areas of issue.

The connection string. Do you have a connection string named "CONN"? Nothing in post 1 would indicate you do.

Personally I just keep mine in the 'app settings'.. and reference it this way.

<appSettings>
   <add key="db" value="data source=...blah..blah..blah" />
</appSettings>

Dim myConnection As SqlClient.SqlConnection = Nothing
myConnection = New SqlClient.SqlConnection(AppSettings.Get("db"))




Notice how the key is thet name I am using with the appsettings.get..

You can test for this pretty easily by putting a break in your code (top of that event?) and run to it to check you even HAVE a connection string.

Then if you get that worked out check to make sure your connection string is even working right.
http://www.connectio...-server-2005-ce

After that recheck your table.. does it exist? Did you bork a column name? The list of potential places you went wrong at is plentiful... and no exception or error message makes this difficult at best.
Was This Post Helpful? 0
  • +
  • -

#10 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 142
  • Joined: 21-January 12

Re: can't insert data into sql through app.config in vb.net

Posted 16 April 2012 - 08:37 AM

based on this :

Try
   Trans.Commit()
      MessageBox.Show("Account successfully created", "Account created", MessageBoxButtons.OK, MessageBoxIcon.Information)
   Catch ex As Exception
      MessageBox.Show("Account not successfully created", "Account creation failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
      Trans.Rollback()
End Try



what CONDITION that make your user can't register/create account?

This post has been edited by artemix22: 16 April 2012 - 08:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1