5 Replies - 1280 Views - Last Post: 13 January 2009 - 05:42 AM Rate Topic: -----

#1 bunny20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-December 08

code in connecting database

Post icon  Posted 13 January 2009 - 02:53 AM

I want to add a data in the database but I don't know the right code in connecting the database.

Below is the code;

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
	protected void Page_Load(object sender, EventArgs e)
	{

	}
	protected void cmdSave_Click(object sender, EventArgs e)
	{
	  
	  string connectionString ="SQLEXPRESS;AttachDbFilename="C:\Documents and Settings\jojo\My Documents\Visual Studio 2005\Projects\Testing\Core\testinglang.mdf";Integrated Security=True;User Instance=True";

			SqlConnection connection = new SqlConnection();

			connection.Open();

			SqlCommand cmd = new SqlCommand("SELECT * FROM [testinglang]",connection);
	
  
	 LastName = Lname.Text;
	 FirstName = Fname.Text;
	 MiddleName = Mname.Text;
	 connection.Update();

		string message = "New User Account Successfully Sav\r\n" + Fname.Text;
		string caption = "User Account";
		MessageBoxButtons buttons = MessageBoxButtons.OK;
		MessageBoxIcon icon = MessageBoxIcon.Information;

	 MessageBox.Show(message, caption, buttons, icon);

	 cmd.ExecuteNonQuery();

	 connection.Close();
	}
}



Can anyone tell me the right way in connecting the database....

Is This A Good Question/Topic? 0
  • +

Replies To: code in connecting database

#2 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: code in connecting database

Posted 13 January 2009 - 03:38 AM

Hi again :)

I like to refer you to PsychoCoder´s SQL Basics in C# Tutorial

There you´ll find anything you need :)

regards

Ben
Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: code in connecting database

Posted 13 January 2009 - 03:39 AM

OK I don't have much time, but in short here it is:

I'm not gonna give you the code directly... but you have't used your connection string.
You should not open the connection right away, you should use a try/catch/finally to do that and in the finally you should close the connection.
You do not use the ExecuteNonQuery() function when you are using a select statement, you should use that when you are using update,insert or delete.

Sorry... I don't have time..
Was This Post Helpful? 0
  • +
  • -

#4 bunny20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 29-December 08

Re: code in connecting database

Posted 13 January 2009 - 04:05 AM

b.ihde ...I tried the code in psychocoder's tutorial

and i got an error. "no overload for method 'string' takes '0' arguments.

below is the code
 public static string GetConnectionString(string strConnection)
	{
		//variable to hold our connection string for returning it
		string strReturn = new string();
		//check to see if the user provided a connection string name
		
			//get the default connection string
			strReturn = ConfigurationManager.ConnectionStrings["testdatastring"].ConnectionString;
 
		//return the connection string to the calling method
		return strReturn;
	}



what does this mean?
Was This Post Helpful? 0
  • +
  • -

#5 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: code in connecting database

Posted 13 January 2009 - 04:31 AM

The strReturn string expects an argument..

string strReturn = new string("teststring"); // 1 argument
string strReturn = new strin(); // 0 arguments



regards

Ben Ihde

This post has been edited by b.ihde: 13 January 2009 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: code in connecting database

Posted 13 January 2009 - 05:42 AM

View Postbunny20, on 13 Jan, 2009 - 06:05 AM, said:

b.ihde ...I tried the code in psychocoder's tutorial

and i got an error. "no overload for method 'string' takes '0' arguments.

below is the code
 public static string GetConnectionString(string strConnection)
	{
		//variable to hold our connection string for returning it
		string strReturn = new string();
		//check to see if the user provided a connection string name
		
			//get the default connection string
			strReturn = ConfigurationManager.ConnectionStrings["testdatastring"].ConnectionString;
 
		//return the connection string to the calling method
		return strReturn;
	}



what does this mean?


there's really no need to use "new string()". use this:

string strReturn = string.Empty;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1