This is a discussion on C# database question within the .Net Development forums, part of the Programming & Scripting category; Hey everyone Peebman2000, semi-beginner programmer. Practicing on C# and i'm trying to connect to the database, but its giving me ...
| |||||||
|
#1
| |||
| |||
| Hey everyone Peebman2000, semi-beginner programmer. Practicing on C# and i'm trying to connect to the database, but its giving me an area for the datacommand.connection line. This is how I code in Vb.net and how I'm trying to code the database connection in C#. Can someone show me why i'm getting and error on my datacommand.connection line? It seems to not like the connectionstrings(Peebman2000). Thanks. C# code Code: protected void Button1_Click(object sender, EventArgs e)
{
{
SqlDataAdapter dataadapter;
SqlCommand datacommand;
dataadapter = new SqlDataAdapter();
datacommand = new SqlCommand();
datacommand.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings("peebman2000").ToString);
//datacommand.CommandText = "Insert INTO alerts (" + "title, " + "alert, " + "time)" + "values ('" + TextBox1.Text + "','" + "" + replaceQuotation(FreeTextBox1.Text) + "','" + "" + System.DateTime.Now + " ')";
//Response.Write(datacommand.CommandText.ToString)
//Response.End()
datacommand.Connection.Open();
try
{
datacommand.ExecuteNonQuery();
}
//Response.Write("data was entered")
catch (Exception ex)
{
//Response.Write("data not entered" & ex.ToString);
Response.End();
}
datacommand.Connection.Close();
}
}
|
|
#2
| ||||
| ||||
| In C#, ToString is a method and will requires the parens: Object.ToString(). VB.NET allows you to be lazy. However, since the connectionstring is already a string, removing the ToString() method will save you some code. Also, I think ConnectionStrings is a collection (if memory serves), and in C# collections are indexed with brackets not parens. (If it's a method, then the syntax is right) |
|
#3
| ||||
| ||||
| Just on other kinda .NETty thing to pass along. I have never found need to use Reponse.Write in .NET. If you have them in there for debugging, I'd consider using the Debug class in the System.Diagnostics name space. The outputs the string to the Immediate Window in Visual Studio; doesn't muck up your page and; you don't (really) have to remove them when you deploy (though you really should). |
|
#4
| |||
| |||
| Thanks Wolfy for the reply, but i'm still struggleing with the connections string, i'm a very new newbie to C#. Below is my code and I keep getting an error. I'm using the name of the connection string from the web.cofig. What am I doing wrong with the sql database connection? Thanks Code: protected void Button1_Click(object sender, EventArgs e)
{
{
SqlDataAdapter dataadapter;
SqlCommand datacommand;
dataadapter = new SqlDataAdapter();
datacommand = new SqlCommand();
ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["peebman2000"];
//datacommand.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings("peebman2000").ToString);
datacommand.Connection = new SqlConnection(set.ConnectionString);
//datacommand.Connection = new SqlConnection("peebman2000");
//datacommand.CommandText = "Insert INTO alerts (" + "title, " + "alert, " + "time)" + "values ('" + TextBox1.Text + "','" + "" + replaceQuotation(FreeTextBox1.Text) + "','" + "" + System.DateTime.Now + " ')";
//Response.Write(datacommand.CommandText.ToString)
//Response.End()
//datacommand.Connection.Open();
//Response.Write("database connected");
try
{
datacommand.Connection.Open();
//datacommand.ExecuteNonQuery();
Response.Write("database connected");
}
//Response.Write("data was entered")
//catch (System.Web.HttpException ex) //(Exception ex)
catch (Exception ex)
{
Response.Write(ex.Message);
//Response.Write("data not entered" & ex.ToString);
Response.End();
}
datacommand.Connection.Close();
}
}
Quote:
|
|
#5
| |||
| |||
| Hey Wolffy I got it to work. I wanted to use the response.writes to just show that the app is able to connect to the database. thanks for the replies. |
|
#6
| ||||
| ||||
| What is the error you're getting? And shouldn't it be: Code: Configurationmanager.ConnectionStrings("peebman2000").ConnectionString
Code: Configurationmanager.ConnectionStrings("peebman2000").ToString
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.If you like it here...throw us a few bones to help support us. Join our Folding team: DeveloperBarn Folding |
|
#7
| |||
| |||
| Well I thought it worked, but its not giving me the correct response. It seems to connect to the database, but using the try block, it response.write( datbase connected) and it also response writes (database not connected Thread was being aborted) I don't understand why it's telling me its connected and it's not connected with "Thread was being aborted") Heres the code without the //commentedout code. Code: protected void Button1_Click(object sender, EventArgs e)
{
{
SqlDataAdapter dataadapter;
SqlCommand datacommand;
dataadapter = new SqlDataAdapter();
datacommand = new SqlCommand();
ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["peebman2000"];
datacommand.Connection = new SqlConnection(set.ConnectionString);
try
{
datacommand.Connection.Open();
Response.Write("database connected");
Response.End();
}
catch (Exception ex)
{
Response.Write("database not connected" + ex.Message);
Response.End();
}
datacommand.Connection.Close();
}
}
|
|
#9
| |||
| |||
| Thanks, I tried it like that and I get this error; Quote:
Code: protected void Button1_Click(object sender, EventArgs e)
{
{
SqlDataAdapter dataadapter;
SqlCommand datacommand;
dataadapter = new SqlDataAdapter();
datacommand = new SqlCommand();
ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["peebman2000"].ConnectionString;
datacommand.Connection = new SqlConnection(set.ConnectionString);
try
{
datacommand.Connection.Open();
Response.Write("database connected");
Response.End();
}
catch (Exception ex)
{
Response.Write("database not connected" + ex.Message);
Response.End();
}
datacommand.Connection.Close();
}
}
|
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |