2014年11月25日 星期二

ConnectDB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

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

        string sqlcmd ="select top 20 id,test_time,title,summary from test";
       

    }

    private void ConnectDB(string ConnectionString,string sqlcmd)
    {
       
          SqlConnection Conn = new SqlConnection();
          Conn.ConnectionString = WebConfigurationManager.ConnectionStrings[ConnectionString].ConnectionString;
          SqlDataReader dr = null;
         
          SqlCommand cmd = new SqlCommand(sqlcmd,Conn);

          try
          {
              Conn.Open();
              dr = cmd.ExecuteReader();
              while (dr.Read())
              {

              }

          }
          catch (Exception ex)
          {
              Response.Write("<b>Error Message----</b>" + ex.ToString());
          }
          finally {
              if (dr != null) {
                  cmd.Cancel();
                  dr.Close();
              }
              if (Conn.State == ConnectionState.Open) {
                  Conn.Close();
                  Conn.Dispose();
              }
         
          }
   
    }
}

要怎麼做到按下按鈕後跳出新視窗(open new window),在新視窗輸入內容(Ex.帳號密碼)後回傳(return value)的效果

http://catchtest.pixnet.net/blog/post/28219055-asp.net-%E4%BD%BF%E7%94%A8%E7%AD%86%E8%A8%98

【JavaScript】在ASP.NET 呼叫 JavaScript

http://irismyself.pixnet.net/blog/post/12388073-%E3%80%90asp.net%E3%80%91%E3%80%90javascript%E3%80%91%E5%9C%A8asp.net-%E5%91%BC%E5%8F%AB-javascript-

套用MasterPage後,如何動態新增控制項

http://www.dotblogs.com.tw/hatelove/archive/2011/11/30/how-to-dynamically-add-controls-in-masterpage.aspx

ASP.NET 神奇FindControl方法

http://weisnote.blogspot.tw/2012/03/aspnet-findcontrol.html

DropDownList

for (int i = 0; i < (int)DropDownList1.Items.Count; i++)
        {
            if (DropDownList1.Items[i].Selected)
            {
                Session["my_color"] = DropDownList1.Items[i].Value;
                Session["my_name"] = TextBox1.Text;

                Response.Redirect("AJAX_Case_2.aspx");   //--前往聊天室的主畫面(AJAX版)
            }
        }

[MSSQL]bcp Unicode 選項 -w

http://msdn.microsoft.com/zh-tw/library/ms188289.aspx

2014年10月14日 星期二

[ASP.NET]觀念

1. 一個ASP.NET網頁,只能有一個表單(<Form>)標籤.
2. 所有的ASP.NET控制項都必須放在
<form runat="server">
...
</form>

[ASP.NET]為GridView 加上光棒效果(use javascript)



1. 在GridView的RowDataBound事件中
2.  加入
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("OnMouseover", "this.style.backgroundColor='#E3EAEB'");
            e.Row.Attributes.Add("OnMouseout", "this.style.backgroundColor='#FFFFFF'");
        }

2014年9月1日 星期一

POI读取Excel如何判断行是不是为空

http://bbs.csdn.net/topics/360265527

[ASP.NET]將控制項Panel內的子控制項Textbox找出加總

   int counter = 0;
        foreach (Control c in Panel1.Controls)
        {
            if (c is TextBox)
            {
                counter += Convert.ToInt32(((TextBox)c).Text);

            }
        }
        Response.Write(counter.ToString());

[ASP.NET]在網頁間傳值

QueryString

 URL?QueryString

 QueryString為key=value


Form

[ASP.NET]編輯資料時,固定位置(MaintainScrollPositionOnPostBack)

為了避免GridView進入編輯模式時,畫面重整後自動跳到最上面的一列.

在 .aspx 中

<%@Page Language="C#"
   
    
     MaintainScrollPositionOnPostBack="true"
%>

2014年8月17日 星期日

抓取web.config 的ConnectionString Code

 System.Configuration.Configuration rootWebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
        System.Configuration.ConnectionStringSettings connString;
        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
        {
            connString =
                rootWebConfig.ConnectionStrings.ConnectionStrings["testConnectionString"];
            if (connString != null)
                Response.Write("connection string = "+
                    connString.ConnectionString);
            else
                Response.Write("No Northwind connection string");
        }

2014年8月13日 星期三

[ASP.NET]改變多個物件的屬性值

object[] tb = {TextBox3,TextBox4,TextBox5,TextBox6};
   
        for (int i =0; i <tb.Length; i++)
        {
            ((TextBox)tb[i]).Visible = true;
         
        }
     

2014年7月18日 星期五

2014年7月15日 星期二

[ASP.NET]網頁導向

方法
1. 超連結
 
2. PostBackURL

3. Response.Redirect("~/Target.aspx")

4. Server.Transfer("~/Target.aspx")  ->good
    隱藏Target.aspx 位址
    效率高

2014年3月31日 星期一

[ASP.NET]修改從資料庫select出的資料存入datatable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
             BindGridView();
 
    }
    private string GetConnectionString(){
        return WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
    }

    private void BindGridView()
    {

        DataTable dt = new DataTable();
   
        SqlConnection connection = new SqlConnection(GetConnectionString());

        try
        {
            connection.Open();
            string sqlStatement = "SELECT * FROM mytest";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
            sqlDa.Fill(dt);
         
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                { dt.Rows[i][1] = ((string)dt.Rows[i][1]).Substring(0,1)+"O O"; }
                 //ds.Tables["test"].Rows[e.RowIndex]["test_time"] = my_test_time.Text;
             
                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();
        }
    }

    private void PrintRows(DataSet dataSet)
    {
        // For each table in the DataSet, print the row values.
        foreach (DataTable table in dataSet.Tables)
        {
            foreach (DataRow row in table.Rows)
            {
                foreach (DataColumn column in table.Columns)
                {
                    Console.WriteLine(row[column]);
                }
            }
        }
    }
}

2014年3月25日 星期二

使用sql2005和vs2008時 利用自動SqlDataSource產生資料Insert時 對應的DbType會自動產生Date而不是DateTime

使用sql2005和vs2008時 利用自動SqlDataSource產生資料Insert時
對應的DbType會自動產生Date而不是DateTime需要手動更新

<InsertParameters>
                <asp:ControlParameter ControlID="TextBox1" DbType="Date" Name="test_time"
                    PropertyName="Text" />
 </InsertParameters>



<InsertParameters>
                <asp:ControlParameter ControlID="TextBox1" DbType="DateTime" Name="test_time"
                    PropertyName="Text" />
 </InsertParameters>



ref:
http://forums.asp.net/t/1538329.aspx

2014年3月10日 星期一

2014年3月8日 星期六

找尋姓李或姓王的學生

select
from
where  name like '[李王]%'

判斷成績是否為NULL

IS NULL

判斷值是否為NULL


學號 成績
S0001 56
S0001 73
S0002 92
S0002 63
S0003 92
S0003 70
S0004 75
S0004 88
S0004 68
S0005 NULL

SELECT *
from  xx
where 成績 IS NULL


不可用    成績 = NULL

2014年3月7日 星期五

匯入資料庫-以TSQL方式

exec sp_attach_db '<<資料庫名稱>>','<<檔案路徑1>>','<<檔案路徑2>>'

ex:
exec sp_attach_db  ‘db_name’ , ’D:\ch4_DB.mdf’’,’D:\ch4_DB_log.ldf’

2014年3月6日 星期四

Visual Stdio 使用

將不知道的物件反白                按F1 可以連上MS的MSDN
DateTime

讓ASP.NET網站在開發除錯時期擁有WEB.CONFIG文件轉換功能

ref: https://blog.kkbruce.net/2016/09/enabling-xml-document-transforms-for-all-asp-net-web-by-develop-time.html#.XoP1KGMzbRY