Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Im developing simple ASP .Net web application in VS2010. for that i need to connect database but it was showing some errors in Connection String.

I writing all my database connection code here. can anybody solve it.

C#
public static DataSet GetData(string Sqlstat)
    {
    string str=@"Data Source=.\SQLEXPRESS;AttachDbFilename="D:\Practice\ASP .Net\WebDb\App_Data\pubs.mdf";Integrated Security=True;User Instance=True";
    SqlConnection cn=new SqlConnection(str);
    SqlDataAdapter da=new SqlDataAdapter(Sqlstat,cn);
    DataSet ds=new DataSet();
    da.Fill(ds);
    return ds;
    }


With following errors

Error    5    Unexpected character '\'    D:\Practice\ASP .Net\WebDb\App_Code\DataHelper.cs    21    96    D:\...\WebDb\
Error    7    Only assignment, call, increment, decrement, and new object expressions can be used as a statement    D:\Practice\ASP .Net\WebDb\App_Code\DataHelper.cs    21    64    D:\...\WebDb\
Error    10    The name 'ASP' does not exist in the current context    D:\Practice\ASP .Net\WebDb\App_Code\DataHelper.cs    21    73    D:\...\WebDb\
Error    17    The name 'pubs' does not exist in the current context    D:\Practice\ASP .Net\WebDb\App_Code\DataHelper.cs    21    97    D:\...\WebDb\


I have mention some errors i didnt mention the similar type errors. Can anybody rectify it.
Posted
Updated 22-Oct-11 20:24pm
v2

Actually its my classroom application but any i want to be good in all.
my requirement is i need to create 3 layers for simple application..
everything is fine but some new problem araise
now i got the following error
Error Summary
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
Im copying all my application code here.
DataHelper.cs
<pre lang="c#">public class DataHelper
{
public DataHelper()
{
//
// TODO: Add constructor logic here
//
}
public static DataSet GetData(String sqlstat)
{
String str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\WebSite\Dbweb\App_Data\pubs.mdf;Integrated Security=True;User Instance=True";
SqlConnection cn = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter(sqlstat, cn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
public static void Execute(String sqlstat)
{
String str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\WebSite\Dbweb\App_Data\pubs.mdf;Integrated Security=True;User Instance=True";
SqlConnection cn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand(sqlstat, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();

}

}</pre>
BLJobs
<pre lang="c#">public class BLJobs
{
public BLJobs()
{
//
// TODO: Add constructor logic here
//
}
public DataSet GetJob()
{
return DataHelper.GetData("select * from Jobs");

}
public void AddJob(String desc, int minlvl, int maxlvl)
{
DataHelper.Execute("insert into Job values('" + desc + "'," + minlvl + "," + maxlvl + ")");
}
}
</pre>
and this is my webform

<pre lang="c#">public partial class Dbweb : System.Web.UI.Page
{

BLJobs obj = new BLJobs();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void LinkButton1_Click(object sender, EventArgs e)
{
GrdView.DataSource = obj.GetJob();
GrdView.DataBind();
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
obj.AddJob(TxtDesc.Text, int.Parse(TxtMinlvl.Text), int.Parse(TxtMaxlvl.Text));
Lbl.Text = "Record has been Added";
}
}</pre>
 
Share this answer
 
v2
Use this line :
C#
string str=@"Data Source=.\SQLEXPRESS;AttachDbFilename=""D:\Practice\ASP .Net\WebDb\App_Data\pubs.mdf"";Integrated Security=True;User Instance=True";


When using @ for strings you must deliminate the " character.
 
Share this answer
 
Comments
Amir Mahfoozi 23-Oct-11 2:43am    
+5
Mehdi Gholam 23-Oct-11 2:54am    
Thanks
Espen Harlinn 23-Oct-11 6:46am    
Right :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900