Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > .NET Framework > Forum > ช่วยดูโค้ดให้หน่อยคับ ทำไมมันถึงไม่ดึงข้อมูลจากฐานข้อมุลมาแสดง



 

ช่วยดูโค้ดให้หน่อยคับ ทำไมมันถึงไม่ดึงข้อมูลจากฐานข้อมุลมาแสดง

 



Topic : 036811

Guest




private void txtProductID_KeyDown_1(object sender, KeyEventArgs e)
{
if (txtProductID.Text == "")
{
return;
}

if (e.KeyCode == Keys.Enter)
{
sb.Remove(0, sb.Length);
sb.Append("SELECT ProductID,Product,Price,ProductBalance");
sb.Append(" FROM Whouse");
sb.Append(" WHERE (ProductID=@ProductID)");
string sqlProduct = sb.ToString();

DataTable dtProduct;
command.CommandType = CommandType.Text;
command.CommandText = sqlProduct;
command.Parameters.Clear();
command.Parameters.Add("@ProductID", SqlDbType.Int).Value = int.Parse(txtProductID.Text);
command.Connection = Conn;
dr = command.ExecuteReader();
if (dr.HasRows)
{
dtProduct = new DataTable();
dtProduct.Load(dr);

txtProductID.Text = dtProduct.Rows[0]["ProductID"].ToString();
lblProductName.Text = dtProduct.Rows[0]["ProductName"].ToString();
lblSalePrice.Text = dtProduct.Rows[0]["UnitPrice"].ToString();
lblbalance.Text = dtProduct.Rows[0]["ProductBalance"].ToString();

CalculateTotal();
txtAmount.Focus();
}
else
{
MessageBox.Show("รหัสสินค้าที่คุณป้อน ไม่ถูกต้อง !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);

ClearProductData();
txtProductID.Focus();
}
dr.Close();
}
}


ตรงที่ขีดเส้นใต้นั้นอ่าคับมันแสดง error ว่า ExecuteReader requires an open and available Connection. The connection's current state is closed.ต้องแก้ยังไงคับ



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-01-11 00:57:22 By : ling View : 6132 Reply : 8
 

 

No. 1



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


Code (C#)
command.Connection = Conn;
Conn.Open(); //<-- เพิ่มตรงนี้
dr = command.ExecuteReader();







Date : 2010-01-11 09:04:05 By : tungman
 


 

No. 2



โพสกระทู้ ( 187 )
บทความ ( 0 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ตามเม้นบนครับ ลองดักเออเร่อดู ในส่วนของการเปอดใช้งานดาต้าเบส ยังไม่ได้เปิดครับ แต่มี dr.Close(); ซะแล้ว

อิอิ
Date : 2010-01-11 09:20:39 By : nongbreesh
 

 

No. 3



โพสกระทู้ ( 96 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณมากคับ

จะลองทำดูน่ะคับ
Date : 2010-01-11 09:44:31 By : ling-keaw
 


 

No. 4

Guest


มันยังติดerror อีกอ่าคับ
มันบอกให้กำหนดค่า The ConnectionString property has not been initialized.ตรงที่ Conn.Open();

งั้นผมเอาดค้ทั้งหมดลงน่ะคับ เผื่อผมลืมลงโค้ดตัวไหนไว้
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace CarBusiness
{
public partial class Sell : Form
{
public Sell()
{
InitializeComponent();
}

SqlCommand command = new SqlCommand();
StringBuilder sb = new StringBuilder();
SqlConnection Conn = new SqlConnection();
SqlDataReader dr;
private void Sell_Load(object sender, EventArgs e)
{
string strCon = "Data Source=.\\SQLExpress;Initial Catalog=car_business;Integrated Security=True";
SqlConnection Conn = new SqlConnection();
if (Conn.State == ConnectionState.Open)
{
Conn.Close();
MessageBox.Show("connect close");
}
Conn.ConnectionString =strCon;
Conn.Open();


lsvProductList.Columns.Add("รหัสสินค้า", 60, HorizontalAlignment.Left);
lsvProductList.Columns.Add("ชื่อสินค้า", 150, HorizontalAlignment.Left);
lsvProductList.Columns.Add("ราคาขาย", 65, HorizontalAlignment.Right);
lsvProductList.Columns.Add("จำนวน", 50, HorizontalAlignment.Right);
lsvProductList.Columns.Add("รวมเป็นเงิน", 70, HorizontalAlignment.Right);
lsvProductList.Columns.Add("สินค้าคงเหลือ", 70, HorizontalAlignment.Right);
lsvProductList.View = View.Details;
lsvProductList.GridLines = true;
lsvProductList.FullRowSelect = true;

txtAmount.ContextMenu = new ContextMenu();
txtProductID.ContextMenu = new ContextMenu();
this.txtCustomerName.Focus();

}
private void ClearCustomerData()
{
txtCustomerName.Text = "";
txtPhone.Text = "";

}
private void ClearProductData()
{
txtProductID.Text = "";
lblProductName.Text = "";
lblSalePrice.Text = "0";
txtAmount.Text = "1";
lblTotal.Text = "0";
lblbalance.Text = "";
}
private void CalculateTotal()
{
double Total;
Total = (double.Parse(lblSalePrice.Text)) * int.Parse(txtAmount.Text);
lblTotal.Text = Total.ToString("#,##0.00");
}
private void CalculateNet()
{
int i = 0;
double tmpNetTotal = 0;
for (i = 0; i <= lsvProductList.Items.Count - 1; i++)
{
tmpNetTotal += double.Parse(lsvProductList.Items[i].SubItems[4].Text);
}
lblNet.Text = tmpNetTotal.ToString("#,##0.00");
}
private void txtProductID_KeyDown_1(object sender, KeyEventArgs e)
{
if (txtProductID.Text == "")
{
return;
}

if (e.KeyCode == Keys.Enter)
{
sb.Remove(0, sb.Length);
sb.Append("SELECT ProductID,Product,Price,ProductBalance");
sb.Append(" FROM Whouse");
sb.Append(" WHERE (ProductID=@ProductID)");
string sqlProduct = sb.ToString();

DataTable dtProduct;
command.CommandType = CommandType.Text;
command.CommandText = sqlProduct;
command.Parameters.Clear();
command.Parameters.Add("@ProductID", SqlDbType.Int).Value = int.Parse(txtProductID.Text);
command.Connection = Conn;
//ConnectionString = ;
Conn.Open();
dr = command.ExecuteReader();
if (dr.HasRows)
{
dtProduct = new DataTable();
dtProduct.Load(dr);

txtProductID.Text = dtProduct.Rows[0]["ProductID"].ToString();
lblProductName.Text = dtProduct.Rows[0]["ProductName"].ToString();
lblSalePrice.Text = dtProduct.Rows[0]["UnitPrice"].ToString();
lblbalance.Text = dtProduct.Rows[0]["ProductBalance"].ToString();

CalculateTotal();
txtAmount.Focus();
}
else
{
MessageBox.Show("รหัสสินค้าที่คุณป้อน ไม่ถูกต้อง !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);

ClearProductData();
txtProductID.Focus();
}
dr.Close();
}
}


private void cmdClear_Click(object sender, EventArgs e)
{
lsvProductList.Items.Clear();
lblNet.Text = "0";
txtProductID.Focus();
ClearCustomerData();
ClearProductData();
}

private void cmdAdd_Click(object sender, EventArgs e)
{
if ((txtProductID.Text == "") || (lblProductName.Text == ""))
{
txtProductID.Focus();
return;
}

if (int.Parse(txtAmount.Text) == 0)
{
txtAmount.Focus();
return;
}

int i = 0;
ListViewItem lvi;
int tmpProductID = 0;
for (i = 0; i <= lsvProductList.Items.Count - 1; i++)
{
tmpProductID = int.Parse(lsvProductList.Items[i].SubItems[0].Text);
if (int.Parse(txtProductID.Text.Trim()) == tmpProductID)
{
MessageBox.Show("คุณเลือกสินค้าซ้ำกัน กรุณาเลือกใหม่ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearProductData();

txtProductID.Focus();
txtProductID.SelectAll();
return;
}
}

string[] anyData;
anyData = new string[] { txtProductID.Text, lblProductName.Text, lblSalePrice.Text, txtAmount.Text, lblTotal.Text, lblbalance.Text };
lvi = new ListViewItem(anyData);
lsvProductList.Items.Add(lvi);

CalculateNet();
ClearProductData();
cmdSave.Enabled = true;
txtProductID.Focus();
}




}
}
Date : 2010-01-11 14:46:40 By : ling-keaw
 


 

No. 5



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


Code (C#)
SqlCommand command = new SqlCommand();
StringBuilder sb = new StringBuilder();
SqlConnection Conn = new SqlConnection();

SqlDataReader dr;
private void Sell_Load(object sender, EventArgs e)
{
string strCon = "Data Source=.\\SQLExpress;Initial Catalog=car_business;Integrated Security=True";
SqlConnection Conn = new SqlConnection(); //<-- ประกาศซ้ำ

if (Conn.State == ConnectionState.Open)
{
Conn.Close();
MessageBox.Show("connect close");
}
Conn.ConnectionString =strCon;
Conn.Open();

Date : 2010-01-11 14:52:17 By : tungman
 


 

No. 6



โพสกระทู้ ( 96 )
บทความ ( 0 )



สถานะออฟไลน์


เง้อ

แก้Errorไม่หมดซะทีมันบอกว่า
ตรงบันทัด Conn.ConnectionString = strCon;--> Not allowed to change the 'ConnectionString' property. The connection's current state is open. แกงัยอ่าคับ
ไม่ไหวแล้ว รบกวนด้วยน่ะคับ
Date : 2010-01-11 15:47:47 By : ling-keaw
 


 

No. 7



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


Code (C#)
DataTable dtProduct;
command.CommandType = CommandType.Text;
command.CommandText = sqlProduct;
command.Parameters.Clear();
command.Parameters.Add("@ProductID", SqlDbType.Int).Value = int.Parse(txtProductID.Text);
command.Connection = Conn;
//ConnectionString = ;
Conn.Open(); //<-- เอานั้นนี้ออก เพราะข้างบน open ไว้แล้ว
dr = command.ExecuteReader();
if (dr.HasRows)
{
dtProduct = new DataTable();
dtProduct.Load(dr);

Date : 2010-01-11 16:20:41 By : tungman
 


 

No. 8



โพสกระทู้ ( 96 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณมากคับ


,,
,,,
,,,,
Total = (double.Parse(lblSalePrice.Text)) * int.Parse(txtAmount.Text); ต้องเขียนรูปแบบไหนคับถึงจะถูกเพราะมันแจ้งว่า Input string was not in a correct format.
Date : 2010-01-11 16:43:13 By : ling-keaw
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ช่วยดูโค้ดให้หน่อยคับ ทำไมมันถึงไม่ดึงข้อมูลจากฐานข้อมุลมาแสดง
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่