将数据库连接到我的网站 [英] connecting database to my website

查看:78
本文介绍了将数据库连接到我的网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常感谢您的帮助...现在我需要另一个帮助....
需要在该表中插入数据....单击按钮....plz帮助....
下面是我以前的问题.....



我正在使用下面的代码将我的数据库连接到gridview.然后在调试后,我得到以下错误....

我还需要做什么?

Thanx all for the help...now I need an another help....
need to insert data in that table....with a button click....plz help....
below was my previous question.....



I am using the below code to connect my database to gridview. then after debugging i am getting the following error....

what else do i need to do?

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
 
    private string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
    }
 
    #region Bind GridView
    private void BindGridView()
    { 
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(GetConnectionString()); 
        try
        {
            connection.Open();
            string sqlStatement = "SELECT Top(10)* FROM Customers";
            SqlCommand cmd = new SqlCommand(sqlStatement, connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
 
              sqlDa.Fill(dt);
              if (dt.Rows.Count > 0)
              {
                GridView1.DataSource = dt;
                GridView1.DataBind();
              }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
                string msg = "Fetch Error:";
                msg += ex.Message;
                throw new Exception(msg);
        }
        finally
        {
            connection.Close();
        }
    }
    #endregion
}





********Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1061: 'ASP.default2_aspx' does not contain a definition for 'GridView1_SelectedIndexChanged' and no extension method 'GridView1_SelectedIndexChanged' accepting a first argument of type 'ASP.default2_aspx' could be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 9:           
Line 10:     <p>
Line 11:         <asp:GridView ID="GridView1" runat="server" 
Line 12:             onselectedindexchanged="GridView1_SelectedIndexChanged">
Line 13:         </p>

推荐答案

只需检查 aspx.cs 代码是否单击了Selected_Changed事件?
&如果不是,请单击gridview&的属性.在事件上,单击Selected_Changed COde以消除错误
希望对您有所帮助.
谢谢
Just Check The aspx.cs code that Selected_Changed event is clicked or not ?
& If not Click the properties of gridview & On Events Click Selected_Changed COde To remove the error
Hope it will be Helpful.
Thanks


SqlConnection conn = new SqlConnection("server=Servername;database=DatabaseName;uid=UserID;pwd=Password");

        try
        {
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Table_Name , conn);

            DataTable dt = new DataTable();
            adapter.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();                
        }
        catch (Exception ex)
        {
           //Exception message
        }
        finally
        {
            conn.Close();
        }


这篇关于将数据库连接到我的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆