Showing posts with label sqlconnection. Show all posts
Showing posts with label sqlconnection. Show all posts

Monday, March 26, 2012

New SQLConnection w/ user specifics......

This actually goes back to another post...I need the user to state which specifics to use when connecting to a server. I have text boxes set up and I need to have thier input placed into the New SqlConnection statement. I have used the below which did not work...

'dim connorthwind as New SQLConnection ("Server=" & strservername & ";UID=" & strUsername & ";PWD=" & strPassword & ";database=master")

See below for my code:

<%@. Page %>
<%@. import Namespace="System.Data.SqlClient" %>
<%@. import Namespace="System.Data" %>
<script runat="server">

Sub Button_Click( s As Object, e As EventArgs )
dim conNorthwind as New SqlConnection( "Server=dbtest1;UID=cbtest;PWD=password3;database=master" )

Dim strInsert As String
Dim cmdInsert As SqlCommand

cmdInsert = New SqlCommand( "sp_password_sec", conNorthwind )
cmdInsert.CommandType = CommandType.StoredProcedure
'cmdINsert.Parameters.Add( "@.loginname", txtname.text )
cmdINsert.Parameters.Add( "@.oldpwd", Password.text )
cmdINsert.Parameters.Add( "@.newpwd", txtnew.text )
conNorthwind.Open()
cmdInsert.ExecuteNonQuery()
conNorthwind.Close()
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="Server">
<h2>Password Change
</h2>
<b>User Name</b>
<br />
<asp:TextBox id="username" Runat="Server"></asp:TextBox>
<p>
<b>Server Name</b>
<br />
<asp:TextBox id="Servername" Runat="Server"></asp:TextBox>
</p>
<p>
<b>Database </b>
<br />
<asp:TextBox id="txtdatabase" Runat="Server"></asp:TextBox>
</p>
<p>
<b>Old Pass</b>
<br />
<asp:TextBox id="Password" Runat="Server"></asp:TextBox>
</p>
<p>
<b>New Pass</b>
<br />
<asp:TextBox id="txtnew" Runat="Server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button1" onclick="Button_Click" Runat="Server" Text="Lookup!"></asp:Button>
</p>
</form>
</body>
</html>

What didn't work? Did you get an error?

Also, this way of connecting is quite unusual, it is a better practice to use the same connection string for everyone for a variety of reasons (eg, to use connection pooling & to maintain the proper security)

|||

...I don't think it would be possible for everyone to use the same connection if they are resetting their own pw. If they log on using a functional acct or system acct, it will not allow them to change b/c they are not logged in using their own acct...

|||

cbaxter82:

if they are resetting their own pw

What pwd are they changing? If they change their windows pwd or their application pwd it has nothing to do with a sql connection. You should not be adding users to SQL Server, that's a major headache when users come and go and it is a major security breach, too (once a user has a SQL login, they can log in via Excel if they're smart enough! You need to control access to the databse).

New Sqlcommand question??

Hello,

I was trying to do the following:

Dim cmd As SqlCommand
Dim objConnection As SqlConnection
objConnection = New SqlConnection = Web.config

where the We.config is where my connection string is set. but I get a sintax error in the Web.config line.

is it possible to asign the value of the web.config content to the new sql connection?

thanks for any suggestions.

Hi, you can use WebConfigurationManager class to get connection string from web.config. Please refer to this article:

http://msdn2.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager(d=ide).aspx

Wednesday, March 7, 2012

new in .Net but learning SqlConnection, SqlCommand and SqlDataReader How in code_behind

Hi.

I want to now a little about CodeBehind how to use it with SqlConnection, SqlCommand and SqlDataReader.

How i use:
Web.Config
<connectionStrings>
<add name="ConnectionStringTest" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DSNbase.mdb;Persist Security Info=True"providerName="System.Data.OleDb"/>
</connectionStrings>

Default.aspx
<asp:RepeaterID="test"runat="server"DataSourceID="SqlDataSourcetest">
...
..
<asp:SqlDataSourceID="SqlDataSourcetest"runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionStringTest %>"
ProviderName="<%$ ConnectionStrings:ConnectionStringTest.ProviderName %>"
SelectCommand="SELECT [ID], [Name], [Sex], [Born], [Color], [Dog], [Images] FROM [Dogs] WHERE ([Dog] = 'YES') ORDER BY [ID]">
</asp:SqlDataSource>

How can i take my asp:SqlDataSource and put it in my CodeBehind, so it use the ConnectionString from my Web.Config and so i can use my Repeater on my default.aspx site.. !?
Maybe if u got a great link that show "How to do" maybe om MSDN/MSDN2.

You can use a datareader. Heres an example i put together using a stored procedure:

#region StoredProcedure GetDogs
// CREATE PROCEDURE GetDogs()
// AS
// SELECT ID, Name, Sex, Born, Color, Dog, Images FROM Dogs WHERE Dog='YES' ORDER BY ID
// RETURN
#endregion

//This will get the connectionstring from the web.config
SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringText"].ConnectionString);
SqlCommand objCmd = new SqlCommand("GetDogs", objConn);
objCmd.CommandType = CommandType.StoredProcedure;

objConn.Open();
SqlDataReader objRdr = objCmd.ExecuteReader(CommandBehavior.CloseConnection);

if (objRdr.HasRows)
{
testRepeater.DataSource = objRdr;
testRepeater.DataBind();
}
else { // do alternative if no data is present. }

objRdr.Close();
objConn.Close();

|||

Hi.

How will this look like if im using VB.

|||

This should work, but im not 100% sure, Im doing this by hand and not in VS, but its striaght forward and you should be able to see how to change it:

//This will get the connectionstring from the web.config
Dim objConn As SqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringText"].ConnectionString)
Dim objCmd As SqlCommand = new SqlCommand("GetDogs", objConn)
objCmd.CommandType = CommandType.StoredProcedure

objConn.Open();
Dim objRdr As SqlDataReader = objCmd.ExecuteReader(CommandBehavior.CloseConnection)

testRepeater.DataSource = objRdr
testRepeater.DataBind()

objRdr.Close()
objConn.Close()

Hope this helps.