1
Reply

How do i load a datatable to a DataGridView control?

petre Ricardo

petre Ricardo

Mar 7 2010 2:53 AM
3.7k
Hi friend,

my client has dataGridveie control and i was trying to load a DataTable, a row of values based on search by code,


here my dal:

 
public static DataTable GetBrands(string code)
{
SqlConnection g_conn;
SqlCommand g_cmd;
DataSet g_ds = new DataSet();
SqlDataAdapter g_da = new SqlDataAdapter();
SqlParameter pRowVersion;
try
{
using (g_conn = new SqlConnection( ConnectionStringBuild.GetConnectionString()) )
{
g_da.SelectCommand =
new SqlCommand();
g_da.SelectCommand.CommandType =
CommandType.StoredProcedure;
g_da.SelectCommand.CommandText =
"TestSearchBrandsByCode";
g_da.SelectCommand.Connection = g_conn;
SqlParameter pBrandCode = new SqlParameter("@PBrandCode", SqlDbType.NChar, 6)
{ Direction =
ParameterDirection.InputOutput, Value = code };
g_da.SelectCommand.Parameters.Add(pBrandCode);
g_da.SelectCommand.Parameters.Add(
"@PBrandID", SqlDbType.Int, -1).Direction = ParameterDirection.Output;
g_da.SelectCommand.Parameters.Add(
"@PShortName", SqlDbType.NVarChar, 50).Direction = ParameterDirection.Output;
g_da.SelectCommand.Parameters.Add(
"@PFullName", SqlDbType.NVarChar, 100).Direction = ParameterDirection.Output;
pRowVersion =
new SqlParameter("@PRowVersion", SqlDbType.VarBinary, -1)
{ Direction =
ParameterDirection.Output, SourceVersion = DataRowVersion.Original };
g_da.SelectCommand.Parameters.Add(pRowVersion);
g_da.SelectCommand.Connection.Open();
g_da.Fill(g_ds);
g_da.SelectCommand.Connection.Close();
return g_ds.Tables[0];
}
}
catch
{
throw;
}

}
here is my SP:

 
ALTER
PROC TestSearchBrandsByCode
@PBrandCode
nchar(6) OUTPUT,
@PBrandID
int OUTPUT,
@PShortName
NVARCHAR(50) OUTPUT,
@PFullName
NVARCHAR(100) OUTPUT,
@PRowVersion
TIMESTAMP OUTPUT
AS
BEGIN

SELECT
BrandID
= @PBrandID,
BrandCode
= @PBrandCode,
ShortName
= @PShortName,
FullName
= @PFullName,
RVersion
= @PRowVersion
FROM tblBrands
WHERE BrandCode = @PBrandCode
END

GO
here is error message:

Photobucket 

How do i fix this error?

thanks

Answers (1)