Closed Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20

Thread: C# database question

  1. #1
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    C# database question

    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. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,037
    Blog Entries
    2
    Rep Power
    13

    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. #3
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    1,037
    Blog Entries
    2
    Rep Power
    13

    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. #4
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    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 Originally Posted by peebman2000 View Post
    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();
            } 
        }
    

  5. #5
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    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. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    What is the error you're getting? And shouldn't it be:
    Code:
    Configurationmanager.ConnectionStrings("peebman2000").ConnectionString
    
    not:
    Code:
    Configurationmanager.ConnectionStrings("peebman2000").ToString
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  7. #7
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    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();
            } 
        }
    

    Quote Originally Posted by jmurrayhead View Post
    What is the error you're getting? And shouldn't it be:
    Code:
    Configurationmanager.ConnectionStrings("peebman2000").ConnectionString
    
    not:
    Code:
    Configurationmanager.ConnectionStrings("peebman2000").ToString
    

  8. #8
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Try like this:
    Code:
    ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["peebman2000"].ConnectionString;
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  9. #9
    Barn Enthusiast peebman2000 is on a distinguished road peebman2000's Avatar
    Join Date
    Mar 2008
    Posts
    210
    Rep Power
    3

    reply

    Thanks, I tried it like that and I get this error;
    Cannot implicitly convert type 'string' to 'System.Configuration.ConnectionStringSettings'
    Here my code again;
    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();
            } 
        }
    
    Quote Originally Posted by jmurrayhead View Post
    Try like this:
    Code:
    ConnectionStringSettings set = ConfigurationManager.ConnectionStrings["peebman2000"].ConnectionString;
    

  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Alright, now give this a shot:
    Code:
    String set = ConfigurationManager.ConnectionStrings["peebman2000"].ConnectionString;
     
    datacommand.Connection = new SqlConnection(set);
    
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


Closed Thread
Page 1 of 2 1 2 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO