Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble logining into sql server 1

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
0
0
CA
I have a solutin with forms that use table adapters to input data into sql tables and that all works fine. My trouble comes when I have another form that uses a sql stored procedure. When I try to connect to the server I get an error that the login failed. Im using the same type of connection as I did for my dataset to login and that was integrated security. Is this problem because I'm already in the data base with the solution and then I try to open another connection with the following code for the execution of a stored procedure? Any help would be greatly apprecieated. I have also used the line Integrated Security = True but this did not work either.

private void approveestBN_Click(object sender, EventArgs e)
{
//string ConnectionString = "Data Source={0};Initial Catalog={1};Integrated Security=True";
string ConnectionString = "Data Source={0};Initial Catalog={1};Trusted_Connection=True";

int estno;
if (int.TryParse(startestnoCB.Text, out estno))
{
SqlConnection conn = new SqlConnection(string.Format(ConnectionString, "server1", "Estimate"));
SqlCommand cmd = new SqlCommand("EstimateApproval", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Estno", estno);
try
{
conn.Open();
#if WantSomethingBack
DataTable result = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(result);
foreach(DataRow row in result.Rows)
{

}
result.Dispose()
#else
cmd.ExecuteNonQuery(); // If you do not need a return;
#endif
}
catch (SqlException err)
{
MessageBox.Show(err.Message);
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
}
}

}

 
I see a couple issues I would avoid, but nothing that looks completely wrong.

1. I would store the connection strings in the app/web config file. i would not dynamically create this at runtime

2. I would create a database connection manager object so i could manage my database connections using the unit of work pattern, instead of creating, using, disposing a new connection every time i needed one.

3. I wouldn't use the declarative #if statements to determine what action I need to take. instead I would explicitly setup objects/members that handle either query (reads) or commands (insert, update, delete) also known as command query separation.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top