locked
SQL Data Source RRS feed

  • Question

  • User2090065235 posted

    Hi, 

    I'm a new  in the world of ASP. I would like to ask Your help in my SQLDataSource question. I created SQLDataSource string in the web config :

    <connectionStrings>
    <add name="MYConnectionString" connectionString="Data Source=Computer/SQLExpress;Initial Catalog=Database1;Integrated Security=True"
    Providername ="System.Data.SqlClient" />
    </connectionStrings>

    How can i use this connection string in my insert ASP website ? 

    I tried this code :

    Imports System.Data.SqlClient

    Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Labeldate.Text = DateTime.Now
    End Sub

    Protected Sub Submit_Click(sender As Object, e As System.EventArgs) Handles Submit.Click
    Using Connection As New SqlConnection("MYConnectionString")
    Connection.Open()

    Dim cmd As New SqlCommand("insert into customers values ('" & txtboxvname.Text & "', '" & txtboxkname.Text & "', '" & txtboxcompname.Text & "', '" & txtboxdate.Text & "')")
    cmd.ExecuteNonQuery()
    Connection.Close()
    End Using

    End Sub
    End Class

    Thanks for Your help !

    Friday, January 2, 2015 2:50 PM

Answers

  • User1428246847 posted

    You need to get the connection string from the configuration. I don't do VB (I know just enough to be dangerous), but possibly you can understand the below C# code and convert to VB

    string connectionstring = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    

    And next you can use that in your using statement

    Using Connection As New SqlConnection(connectionstring)
        Connection.Open()
        ...
        ...

    You need to add a reference to System.Configuration in the references of your project. And an imports statement like

    Imports System.Configuration
    

    Hope it helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2015 3:06 PM

All replies

  • User1428246847 posted

    You need to get the connection string from the configuration. I don't do VB (I know just enough to be dangerous), but possibly you can understand the below C# code and convert to VB

    string connectionstring = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
    

    And next you can use that in your using statement

    Using Connection As New SqlConnection(connectionstring)
        Connection.Open()
        ...
        ...

    You need to add a reference to System.Configuration in the references of your project. And an imports statement like

    Imports System.Configuration
    

    Hope it helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 2, 2015 3:06 PM
  • User2090065235 posted

    Hi, 

    It is working, thanks for Your help !

    Saturday, January 3, 2015 3:53 AM