10 Replies - 1012 Views - Last Post: 10 May 2011 - 08:06 PM Rate Topic: -----

#1 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Can't connect to mssql

Posted 03 May 2011 - 02:08 AM

Hi..

I want to add data into sql database but I can't make connection on it.

Below codes are my application config used in vb.net:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  
  
  <connectionStrings>
    <add name="sample.My.MySettings.EmployeesConnectionString" connectionString="Data Source=CYRUS-PC;Initial Catalog=Employees;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="DBConn" value="Data Source=CYRUS-PC,1433;Network=DBMSSOCN;Initial Catalog=Employees;Integrated Security=True" />

...............
...............
...............

and using
 [code]constring = Configuration.ConfigurationSettings.AppSettings("DBConn")

I could simply connect it in vb.net.

But how could I use these in asp.net? Please help me. Thank you....

This post has been edited by Elda: 03 May 2011 - 02:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't connect to mssql

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,774
  • Joined: 12-June 08

Re: Can't connect to mssql

Posted 03 May 2011 - 07:02 AM

Where's your objects? SQLCommand, SQLConnection, and so for? You know you can right click on your aspx file and click 'view code' to access the vb.net code behind, right?
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Can't connect to mssql

Posted 03 May 2011 - 02:27 PM

or just hit F7
Was This Post Helpful? 0
  • +
  • -

#4 SurfingShark  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 188
  • Joined: 18-April 11

Re: Can't connect to mssql

Posted 03 May 2011 - 05:32 PM

How sure are you your connection string is correct? If you have access to SSMS then replicate what your connection string has to try to connect to the database.

In your code behind have something like this:
Dim connMyConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("my_conn_string_name"))



If your connection string in your web.config file is correct, then you should be able to successfully open that SqlConnection.
Was This Post Helpful? 0
  • +
  • -

#5 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Can't connect to mssql

Posted 03 May 2011 - 08:14 PM

This is what I have done in my aspx file.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Text"%>

<script runat="server">

    Dim MyConnection As SqlConnection

    Sub Page_Load(ByVal Src As Object, ByVal e As EventArgs)
     
        Dim dbconn As SqlConnection
        Dim dbCMD As SqlCommand
        dbconn = New SqlConnection("Data Source=CYRUS-PC;Initial Catalog=samplesys;User integrated security=True")
     
        dbconn.Open()
        
        dbCMD = New SqlCommand("INSERT INTO aspnet (aspId,Firstname,Lastname) VALUES (@aspId,@Firstname,@Lastname)", dbconn)
       
        dbCMD.Parameters.Add(New SqlParameter("@aspId", SqlDbType.NVarChar, 50))
        dbCMD.Parameters.Add(New SqlParameter("@Firstname", SqlDbType.NVarChar, 50))
        dbCMD.Parameters.Add(New SqlParameter("@Lastname", SqlDbType.NVarChar, 50))
        dbCMD.Connection.Open()

        
        dbCMD.ExecuteNonQuery()
        dbconn.Close()
        
    End Sub
</script>



But got error "Keyword not supported: 'user integrated security'." on
dbconn = ......


Please help me fix problem... :(
It's my second day to learn asp. I'm just newbie...
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,774
  • Joined: 12-June 08

Re: Can't connect to mssql

Posted 04 May 2011 - 07:05 AM

Did you take anyone's suggestions? Right click your aspx file and do 'view code' or hit F7. That takes you to the .VB file behind your code. Put it there!
Was This Post Helpful? 0
  • +
  • -

#7 SurfingShark  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 188
  • Joined: 18-April 11

Re: Can't connect to mssql

Posted 04 May 2011 - 03:16 PM

First off, you have SqlParameters but you are not setting any values. So even if your connection did work you wouldn't be happy with the results.

It's not 'user integrated security', it's just 'integrated security'. Please reference www.connectionstrings.com

They walk you through the steps for numerous datastores, including SQL Server. Copy and paste after you put in your conn string params.
Was This Post Helpful? 0
  • +
  • -

#8 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Can't connect to mssql

Posted 09 May 2011 - 09:09 PM

Hi..

I have this code in my vb.

sqlstmt = "INSERT INTO aspnet (aspId,Firstname,Lastname) VALUES (@aspId,@Firstname,@Lastname)"
 constring = "<%$ ConnectionStrings:samplesysConnectionString4 %>"

        cn = New SqlConnection(constring)
        cmd = New SqlCommand(sqlstmt, cn)

        cmd.Parameters.AddWithValue("@aspId", txtId.Text)
        cmd.Parameters.AddWithValue("@Firstname", txtFirstname.Text)
        cmd.Parameters.AddWithValue("@Lastname", txtLastname.Text)

cn.Open()

        cmd.ExecuteNonQuery()
        cn.Close()


        cmd.Dispose()


And why I'm getting this error:
 Format of the initialization string does not conform to specification starting at index 0.
with this line
cn = New SqlConnection(constring)


Please help. thanks...
Was This Post Helpful? 0
  • +
  • -

#9 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 445
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Can't connect to mssql

Posted 10 May 2011 - 05:18 AM

Accessing web.config connection strings in VB.Net
Was This Post Helpful? 1
  • +
  • -

#10 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Can't connect to mssql

Posted 10 May 2011 - 02:14 PM

@Elda , that is very tidy VB.net code there :^:

i looks far away from VB6

Although there is room for improvement

This post has been edited by marinus: 10 May 2011 - 02:17 PM

Was This Post Helpful? 0
  • +
  • -

#11 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Can't connect to mssql

Posted 10 May 2011 - 08:06 PM

It works with this
connStr = ConfigurationManager.ConnectionStrings("samplesysConnectionString").ConnectionString


Thank you...:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1