954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

filling combobox with data stored in a table in sql server

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

diana_j86
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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?

williamrojas78
Junior Poster
111 posts since Jun 2005
Reputation Points: 23
Solved Threads: 10
 

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

preetham.saroja
Junior Poster in Training
82 posts since Jun 2007
Reputation Points: 5
Solved Threads: 1
 

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

tbanisida
Newbie Poster
7 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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 = ""

kazekagerandy
Light Poster
45 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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

tbanisida
Newbie Poster
7 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

can you please make it clear, i cant understand what you want to do.

kazekagerandy
Light Poster
45 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You