943,549 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 24692
  • VB.NET RSS
Oct 7th, 2007
0

filling combobox with data stored in a table in sql server

Expand Post »
How to fill a combobox with a data stored in a table in sql server?....
Can we use stored procedure to do so?

I tryed this code but it keeps giving an error message:
"An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll"
What does that mean????

My code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim connString As String = "server=(local);database=AdvantEdge;trusted_connection=yes"
Dim conn As New SqlConnection(connString)
' fillComboBox1()
Dim strSQL As String = "Select * From Disk"
Dim DA As New SqlDataAdapter(strSQL, conn)
Dim DS As New DataSet
DA.Fill(DS, "Disk")
'Create and populate the DataTable to bind to the ComboBox:
Dim dt As New DataTable
dt.Columns.Add("Disk_Name", GetType(System.String))
dt.Columns.Add("Disk_Key", GetType(System.String))
' Populate the DataTable to bind to the Combobox.
Dim drDSRow As DataRow
Dim drNewRow As DataRow
For Each drDSRow In DS.Tables("Disk").Rows()
drNewRow = dt.NewRow()
drNewRow("Disk_Name") = drDSRow("Disk_Name")
drNewRow("Disk_Key") = drDSRow("Disk_Key")
dt.Rows.Add(drNewRow)
Next
'Bind the DataTable to the ComboBox by setting the Combobox's DataSource property to the DataTable. To display the "Description" column in the Combobox's list, set the Combobox's DisplayMember property to the name of column. Likewise, to use the "Code" column as the value of an item in the Combobox set the ValueMember property.
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
With ComboBox1
.DataSource = dt
.DisplayMember = "Disk_Name"
.ValueMember = "Disk_Key"
.SelectedIndex = 0
End With
End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
diana_j86 is offline Offline
2 posts
since Oct 2007
Oct 9th, 2007
0

Re: filling combobox with data stored in a table in sql server

The error message most likely means that there is an error in one of the sql statements.
which line do you get the error message? which SQL server are you using?
Reputation Points: 23
Solved Threads: 10
Junior Poster
williamrojas78 is offline Offline
111 posts
since Jun 2005
Oct 11th, 2007
0

Re: filling combobox with data stored in a table in sql server

just use this code& place loaddrp() in page_load
where tablename is"table1" & field to b populated inside dropdownlist is "ID"......

Public Function loaddrp() As String
Dim str As String = "select ID from table1"
Dim con As String = ConfigurationManager.AppSettings("preeconn")
Dim com As New SqlCommand(str, New SqlConnection(con))
com.Connection.Open()
Dim ds As New SqlDataAdapter(str, con)
Dim da As SqlDataReader
'ds.Fill(da, "table1")
da = com.ExecuteReader
While da.Read
drp1.Items.Add(da("ID"))
End While
End Function


===========
regards....
preetham
Reputation Points: 5
Solved Threads: 1
Junior Poster in Training
preetham.saroja is offline Offline
82 posts
since Jun 2007
Apr 22nd, 2011
0
Re: filling combobox with data stored in a table in sql server
Hai friends,

I have a table tb_credit fields are


credit_id
date
customer_id
account
amount current_balance

I want to display the current balance of a particular customer in billing form ,
I wrote stored procedure
ALTER PROCEDURE usp_Select_BillNoCredit
(
@customer_id bigint,
@available float output
)
as
begin
select @available = tb_credit.current_balance from tb_credit where credit_id = (select max(credit_id) from tb_credit where tb_credit.customer_id = @customer_id)
end

In business layer

public static double SelectAvailableBalance(long _cid)
{
try
{

SqlParameter[] arParams = new SqlParameter[2];
arParams[0] = new SqlParameter("@customer_id", _cid);
arParams[1] = new SqlParameter("@current_balance", SqlDbType.Float);
arParams[1].Direction = ParameterDirection.Output;
SqlHelper.ExecuteNonQuery(StaticInfo.SqlConnectionString, "usp_Select_BillNoCredit", arParams);
return Convert.ToDouble(arParams[1].Value);
}
catch (Exception oEx)
{
MessageBox.Show(oEx.Message, StaticInfo.MsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
return 0;
}

}

in code name changed,

private void cbname_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
lblAviBal.Text = Convert.ToString(bizBillReport.SelectAvailableBalance(Convert.ToInt32(cbname.SelectedValue)));

}
catch (Exception oEx)
{
MessageBox.Show(oEx.Message, StaticInfo.MsgBoxCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

//PostGridBill();
}

But cant retrive the balance

help me friends this is correct or not
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tbanisida is offline Offline
6 posts
since Mar 2011
Apr 22nd, 2011
0
Re: filling combobox with data stored in a table in sql server
do you mean you want to fill in the display texts of the combobox with the column values from the database? if i am correct, try this code:

Dim conn As New SqlConnection("Data Source = .\sqlexpress ; Initial Catalog = dbname ; Integrated Security = SSPI ")

conn.Open()

Dim sql As New SqlCommand("Select * from disk", conn)
sql.CommandType = CommandType.Text

Dim adapt As New SqlDataAdapter
adapt.SelectCommand = sql
adapt.SelectCommand.ExecuteNonQuery()

Dim dset As New DataSet
adapt.Fill(dset, "disk")

conn.Close()
ComboBox1.DataSource = dset.Tables("disk")
ComboBox1.DisplayMember = "<fieldname>"
Reputation Points: 10
Solved Threads: 0
Light Poster
kazekagerandy is offline Offline
45 posts
since Feb 2011
Apr 22nd, 2011
0

Hai friend

i asked not like this, what i want means if i select the name, that time show the current balance of a particular customer name, please help me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tbanisida is offline Offline
6 posts
since Mar 2011
Apr 23rd, 2011
0
Re: filling combobox with data stored in a table in sql server
can you please make it clear, i cant understand what you want to do.
Reputation Points: 10
Solved Threads: 0
Light Poster
kazekagerandy is offline Offline
45 posts
since Feb 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: crystal report formula error for adding two values and get sum
Next Thread in VB.NET Forum Timeline: Accessing Password Protected Zip File Contents with VB.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC