hi,
I have been doing my ASP.net project in C#.I am a beginner to a programming world.i have Performed executed some sql queries by writing some Connection strings..But i am looking for finding another method,which i could get simpler by calling some function like "studadapter.fill(dataset,"src table")" which is to fill in the Grid view.Please Help me,get some idea about data adapter and its connection..I'm also attaching the source code along with that...

Anyway,thanks in advance..:$

Recommended Answers

All 5 Replies

ANyone please help mee.....:(

have you tried <connection string> method in web.config?
if yes i will show you another but in case you can deal with the same method which we use in ADO.Net
if you are using Gridview control
Gridview1.DataSource = ds; //Dataset object
Gridview1.DataBind(); //Establishes mapping between the data column
//in dataset & those in gridview control

sorry,that's already got by me. i said that, adding a new dataset from adding new data item..and double clicking the dataset(.xsd) and dragging the dataset to the component desginer view,,using a function with a adpater obtained during dragging that dataset... to obtain different query results...(studenttableadapter.Fill() or studenttableadapter.getdataby()....)

here are a couple of suggestions to help you. But first a disclaimer. I'm writing all this in VS2008, it appears you are using VS2005. So while these are correct for my version, I haven't checked for yours. However they should be correct.
First, as mentioned above, use the web config for the connection string.
e.g.

<connectionStrings>
    <add name="constr" connectionString="Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\employee.mdf;Integrated Security=True;User Instance=True;"/>
  </connectionStrings>

To access it in your code behind, add System.Configuration to your usings clause (this may be the difference. You may need to reference System.Configuration 2.0 first before adding to your usings clause) then do this:

string connectionstring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

here is one problem I saw, you should not use
'reader = comm.ExecuteReader();' to execute an insert statement. Instead use comm.ExecuteNonQuery();

I will say one thing about your code. It looks to me you are really trying to follow the principles of OO coding. For that I commend you. So here is another idea ... separate your data access to a different class

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DAO
/// </summary>
public class DAO
{
    private string connectionstring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    private SqlConnection con;

    public string errText = "";

    public DAO()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public DataTable GetDataTable(string qry)
    {
        DataTable dt;
        try
        {
            con = new SqlConnection(connectionstring);
            SqlDataAdapter da = new SqlDataAdapter(qry, con);
            dt = new DataTable();
            da.Fill(dt);
        }
        catch (SqlException ex)
        {
            errText = ex.Message;
            dt = null;
        }
        return dt;
    }

    public int InsertUpdateData(string qry)
    {
        int rowaffected = 0;
        try
        {
            con = new SqlConnection(connectionstring);
            SqlCommand conn = new SqlCommand(qry, con);
            conn.CommandType = CommandType.Text;
            rowaffected = conn.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            errText = ex.Message;
            rowaffected = - 1;
        }
        return rowaffected;
    }

}

Then your selecttemp method would only need to be this

protected void selectemp(string s, int isa)
{
DAO dao = new DAO();
if (isa == 1)
{
dao.InsertUpdateData(s);
}
else
{
DataTable dt = dao.GetDataTable(s);
emplgrid.DataSource = dt;
emplgrid.DataBind();
}
}

As a final comment, you are passing inline sql. Thats not good. But its a starting place. When you get this mastered, you need to learn how to write stored procedures and start using parameterized queries.

Good Luck!

thanks edepperson, for ur Kind reply..
I'll check it and verify whether it's working or not....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.